File: lock.h

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (113 lines) | stat: -rw-r--r-- 2,853 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2012 Shlomi Fish
 */
/*
 * lock.h - macros/functions for locking.
 */
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <pthread.h>

/*
 * Define FCS_DBM_USE_RWLOCK to use the pthread FCFS RWLock which appears
 * to improve CPU utilisations of the various worker threads and possibly
 * overall performance.
 * #define FCS_DBM_USE_RWLOCK 1 */

#ifdef FCS_DBM_USE_RWLOCK
#include <pthread/rwlock_fcfs.h>
#endif

#include "rinutils.h"

#ifdef FCS_DBM_SINGLE_THREAD

typedef bool fcs_lock;
static inline void fcs_lock_lock(fcs_lock *const lock) {}
static inline void fcs_lock_unlock(fcs_lock *const lock) {}
static inline void fcs_lock_init(fcs_lock *const lock) {}
static inline void fcs_lock_destroy(fcs_lock *const lock) {}
#elif defined(FCS_DBM_USE_RWLOCK)

typedef pthread_rwlock_fcfs_t *fcs_lock;
static inline void fcs_lock_lock(fcs_lock *const lock)
{
    pthread_rwlock_fcfs_gain_write(*lock);
}
static inline void fcs_lock_unlock(fcs_lock *const lock)
{
    pthread_rwlock_fcfs_release(*lock);
}
static inline void fcs_lock_init(fcs_lock *const lock)
{
    *lock = pthread_rwlock_fcfs_alloc();
}
static inline void fcs_lock_destroy(fcs_lock *const lock)
{
    pthread_rwlock_fcfs_destroy(*lock);
}

#else

static const pthread_mutex_t initial_mutex_constant = PTHREAD_MUTEX_INITIALIZER;
static const pthread_cond_t initial_cond_constant = PTHREAD_COND_INITIALIZER;

typedef pthread_mutex_t fcs_lock;
typedef pthread_cond_t fcs_condvar;
static inline void fcs_lock_lock(fcs_lock *const lock)
{
    pthread_mutex_lock(lock);
}
static inline void fcs_lock_init(fcs_lock *const lock)
{
    *lock = initial_mutex_constant;
}
static inline void fcs_lock_unlock(fcs_lock *const lock)
{
    pthread_mutex_unlock(lock);
}
static inline void fcs_lock_destroy(fcs_lock *const lock)
{
    pthread_mutex_destroy(lock);
}
static inline void fcs_condvar_init(fcs_condvar *const cond)
{
    *cond = initial_cond_constant;
    pthread_cond_init(cond, NULL);
}

static inline void fcs_condvar_destroy(fcs_condvar *const cond)
{
    pthread_cond_destroy(cond);
}

static inline void fcs_condvar__wait_on(
    fcs_condvar *const cond, fcs_lock *const lock)
{
    pthread_cond_wait(cond, lock);
}

static inline void fcs_condvar_signal(fcs_condvar *const cond)
{
    pthread_cond_signal(cond);
}

static inline void fcs_condvar_broadcast(fcs_condvar *const cond)
{
    pthread_cond_broadcast(cond);
}
#endif

#ifdef __cplusplus
};
#endif