1 | #include "globals.h"
|
---|
2 | #include "oscam-lock.h"
|
---|
3 | #include "oscam-time.h"
|
---|
4 |
|
---|
5 | extern char *LOG_LIST;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * creates a lock
|
---|
9 | **/
|
---|
10 | void cs_lock_create(CS_MUTEX_LOCK *l, int16_t timeout, const char *name)
|
---|
11 | {
|
---|
12 | memset(l, 0, sizeof(CS_MUTEX_LOCK));
|
---|
13 | l->timeout = timeout;
|
---|
14 | l->name = name;
|
---|
15 | pthread_mutex_init(&l->lock, NULL);
|
---|
16 | pthread_cond_init(&l->writecond, NULL);
|
---|
17 | pthread_cond_init(&l->readcond, NULL);
|
---|
18 | #ifdef WITH_MUTEXDEBUG
|
---|
19 | cs_debug_mask_nolock(D_TRACE, "lock %s created", name);
|
---|
20 | #endif
|
---|
21 | }
|
---|
22 |
|
---|
23 | void cs_lock_destroy(CS_MUTEX_LOCK *l)
|
---|
24 | {
|
---|
25 | if (!l || !l->name || l->flag) return;
|
---|
26 |
|
---|
27 | cs_rwlock_int(l, WRITELOCK);
|
---|
28 | #ifdef WITH_DEBUG
|
---|
29 | const char *old_name = l->name;
|
---|
30 | #endif
|
---|
31 | l->name = NULL; //No new locks!
|
---|
32 | cs_rwunlock_int(l, WRITELOCK);
|
---|
33 |
|
---|
34 | //Do not destroy when having pending locks!
|
---|
35 | int32_t n = (l->timeout/10)+2;
|
---|
36 | while ((--n>0) && (l->writelock || l->readlock)) cs_sleepms(10);
|
---|
37 |
|
---|
38 | cs_rwlock_int(l, WRITELOCK);
|
---|
39 | l->flag++; //No new unlocks!
|
---|
40 | cs_rwunlock_int(l, WRITELOCK);
|
---|
41 |
|
---|
42 | #ifdef WITH_DEBUG
|
---|
43 | if (!n && old_name != LOG_LIST)
|
---|
44 | cs_log_nolock("WARNING lock %s destroy timed out.", old_name);
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | pthread_mutex_destroy(&l->lock);
|
---|
48 | pthread_cond_destroy(&l->writecond);
|
---|
49 | pthread_cond_destroy(&l->readcond);
|
---|
50 | #ifdef WITH_MUTEXDEBUG
|
---|
51 | cs_debug_mask_nolock(D_TRACE, "lock %s destroyed", l->name);
|
---|
52 | #endif
|
---|
53 | }
|
---|
54 |
|
---|
55 | void cs_rwlock_int(CS_MUTEX_LOCK *l, int8_t type) {
|
---|
56 | struct timespec ts;
|
---|
57 | int8_t ret = 0;
|
---|
58 |
|
---|
59 | if (!l || !l->name || l->flag)
|
---|
60 | return;
|
---|
61 |
|
---|
62 | ts.tv_sec = time(NULL) + l->timeout;
|
---|
63 | ts.tv_nsec = 0;
|
---|
64 |
|
---|
65 | pthread_mutex_lock(&l->lock);
|
---|
66 |
|
---|
67 | if (type == WRITELOCK) {
|
---|
68 | l->writelock++;
|
---|
69 | // if read- or writelock is busy, wait for unlock
|
---|
70 | if (l->writelock > 1 || l->readlock > 0)
|
---|
71 | ret = pthread_cond_timedwait(&l->writecond, &l->lock, &ts);
|
---|
72 | } else {
|
---|
73 | l->readlock++;
|
---|
74 | // if writelock is busy, wait for unlock
|
---|
75 | if (l->writelock > 0)
|
---|
76 | ret = pthread_cond_timedwait(&l->readcond, &l->lock, &ts);
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (ret > 0) {
|
---|
80 | // lock wasn't returned within time, assume locking thread to
|
---|
81 | // be stuck or finished, so enforce lock.
|
---|
82 | l->writelock = (type==WRITELOCK) ? 1 : 0;
|
---|
83 | l->readlock = (type==WRITELOCK) ? 0 : 1;
|
---|
84 | #ifdef WITH_DEBUG
|
---|
85 | if (l->name != LOG_LIST)
|
---|
86 | cs_log_nolock("WARNING lock %s (%s) timed out.", l->name, (type==WRITELOCK)?"WRITELOCK":"READLOCK");
|
---|
87 | #endif
|
---|
88 | }
|
---|
89 |
|
---|
90 | pthread_mutex_unlock(&l->lock);
|
---|
91 | #ifdef WITH_MUTEXDEBUG
|
---|
92 | //cs_debug_mask_nolock(D_TRACE, "lock %s locked", l->name);
|
---|
93 | #endif
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | void cs_rwunlock_int(CS_MUTEX_LOCK *l, int8_t type) {
|
---|
98 |
|
---|
99 | if (!l || l->flag) return;
|
---|
100 |
|
---|
101 | pthread_mutex_lock(&l->lock);
|
---|
102 |
|
---|
103 | if (type == WRITELOCK)
|
---|
104 | l->writelock--;
|
---|
105 | else
|
---|
106 | l->readlock--;
|
---|
107 |
|
---|
108 | if (l->writelock < 0) l->writelock = 0;
|
---|
109 | if (l->readlock < 0) l->readlock = 0;
|
---|
110 |
|
---|
111 | // waiting writelocks always have priority. If one is waiting, signal it
|
---|
112 | if (l->writelock)
|
---|
113 | pthread_cond_signal(&l->writecond);
|
---|
114 | // Otherwise signal a waiting readlock (if any)
|
---|
115 | else if (l->readlock && type != READLOCK)
|
---|
116 | pthread_cond_broadcast(&l->readcond);
|
---|
117 |
|
---|
118 | pthread_mutex_unlock(&l->lock);
|
---|
119 |
|
---|
120 | #ifdef WITH_MUTEXDEBUG
|
---|
121 | #ifdef WITH_DEBUG
|
---|
122 | if (l->name != LOG_LIST) {
|
---|
123 | const char *typetxt[] = { "", "write", "read" };
|
---|
124 | cs_debug_mask_nolock(D_TRACE, "%slock %s: released", typetxt[type], l->name);
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 | int8_t cs_try_rwlock_int(CS_MUTEX_LOCK *l, int8_t type) {
|
---|
131 | if (!l || !l->name || l->flag)
|
---|
132 | return 0;
|
---|
133 |
|
---|
134 | int8_t status = 0;
|
---|
135 |
|
---|
136 | pthread_mutex_lock(&l->lock);
|
---|
137 |
|
---|
138 | if (type==WRITELOCK) {
|
---|
139 | if (l->writelock || l->readlock)
|
---|
140 | status = 1;
|
---|
141 | else
|
---|
142 | l->writelock++;
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | if (l->writelock)
|
---|
146 | status = 1;
|
---|
147 | else
|
---|
148 | l->readlock++;
|
---|
149 | }
|
---|
150 |
|
---|
151 | pthread_mutex_unlock(&l->lock);
|
---|
152 |
|
---|
153 | #ifdef WITH_MUTEXDEBUG
|
---|
154 | #ifdef WITH_DEBUG
|
---|
155 | if (l->name != LOG_LIST) {
|
---|
156 | const char *typetxt[] = { "", "write", "read" };
|
---|
157 | cs_debug_mask_nolock(D_TRACE, "try_%slock %s: status=%d", typetxt[type], l->name, status);
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 | #endif
|
---|
161 | return status;
|
---|
162 | }
|
---|