-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpth-sem.patch
More file actions
212 lines (204 loc) · 6.58 KB
/
pth-sem.patch
File metadata and controls
212 lines (204 loc) · 6.58 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
diff -urN pth-orig/pth.h.in pth/pth.h.in
--- pth-orig/pth.h.in Sat Jan 1 20:15:05 2005
+++ pth/pth.h.in Wed Jan 12 10:13:29 2005
@@ -187,6 +187,7 @@
#define PTH_EVENT_COND _BIT(7)
#define PTH_EVENT_TID _BIT(8)
#define PTH_EVENT_FUNC _BIT(9)
+#define PTH_EVENT_SEM _BIT(10)
/* event occurange restrictions */
#define PTH_UNTIL_OCCURRED _BIT(11)
@@ -197,6 +198,8 @@
#define PTH_UNTIL_TID_READY _BIT(16)
#define PTH_UNTIL_TID_WAITING _BIT(17)
#define PTH_UNTIL_TID_DEAD _BIT(18)
+#define PTH_UNTIL_DECREMENT _BIT(19)
+#define PTH_UNTIL_COUNT _BIT(23)
/* event structure handling modes */
#define PTH_MODE_REUSE _BIT(20)
@@ -264,6 +267,10 @@
#define PTH_COND_HANDLED _BIT(3)
#define PTH_COND_INIT { PTH_COND_INITIALIZED, 0 }
+ /* semaphore variable values */
+#define PTH_SEM_INITIALIZED _BIT(0)
+#define PTH_SEM_INIT { PTH_SEM_INITIALIZED, 0 }
+
/* barrier variable values */
#define PTH_BARRIER_INITIALIZED _BIT(0)
#define PTH_BARRIER_INIT(threshold) { PTH_BARRIER_INITIALIZED, \
@@ -311,6 +318,13 @@
unsigned int cn_waiters;
};
+ /* the semaphore variable structure */
+typedef struct pth_sem_st pth_sem_t;
+struct pth_sem_st { /* not hidden to avoid destructor */
+ unsigned long sem_state;
+ unsigned int sem_count;
+};
+
/* the barrier variable structure */
typedef struct pth_barrier_st pth_barrier_t;
struct pth_barrier_st { /* not hidden to avoid destructor */
@@ -501,6 +515,13 @@
extern int pth_cond_notify(pth_cond_t *, int);
extern int pth_barrier_init(pth_barrier_t *, int);
extern int pth_barrier_reach(pth_barrier_t *);
+extern int pth_sem_init(pth_sem_t *);
+extern int pth_sem_dec(pth_sem_t *);
+extern int pth_sem_dec_value(pth_sem_t *, unsigned);
+extern int pth_sem_inc(pth_sem_t *, int);
+extern int pth_sem_inc_value(pth_sem_t *, unsigned, int);
+extern int pth_sem_set_value(pth_sem_t *, unsigned);
+extern int pth_sem_get_value(pth_sem_t *, unsigned *);
/* user-space context functions */
extern int pth_uctx_create(pth_uctx_t *);
diff -urN pth-orig/pth_event.c pth/pth_event.c
--- pth-orig/pth_event.c Sat Jan 1 20:15:06 2005
+++ pth/pth_event.c Thu Jan 13 14:01:05 2005
@@ -51,6 +51,7 @@
struct { pth_cond_t *cond; } COND;
struct { pth_t tid; } TID;
struct { pth_event_func_t func; void *arg; pth_time_t tv; } FUNC;
+ struct { pth_sem_t *sem; unsigned count; } SEM;
} ev_args;
};
@@ -204,6 +205,20 @@
ev->ev_args.FUNC.arg = va_arg(ap, void *);
ev->ev_args.FUNC.tv = va_arg(ap, pth_time_t);
}
+ else if (spec & PTH_EVENT_SEM) {
+ /* semaphore variable */
+ pth_sem_t *sem = va_arg(ap, pth_sem_t *);
+ if (spec & PTH_UNTIL_COUNT)
+ ev->ev_args.SEM.count = va_arg(ap, unsigned);
+ else
+ ev->ev_args.SEM.count = 1;
+ ev->ev_type = PTH_EVENT_SEM;
+ if (spec & PTH_UNTIL_DECREMENT)
+ ev->ev_goal = PTH_UNTIL_DECREMENT;
+ else
+ ev->ev_goal = PTH_UNTIL_OCCURRED;
+ ev->ev_args.SEM.sem = sem;
+ }
else
return pth_error((pth_event_t)NULL, EINVAL);
@@ -277,6 +292,11 @@
*arg = ev->ev_args.FUNC.arg;
*tv = ev->ev_args.FUNC.tv;
}
+ else if (ev->ev_type & PTH_EVENT_SEM) {
+ /* condition variable */
+ pth_sem_t **sem = va_arg(ap, pth_sem_t **);
+ *sem = ev->ev_args.SEM.sem;
+ }
else
return pth_error(FALSE, EINVAL);
va_end(ap);
diff -urN pth-orig/pth_sched.c pth/pth_sched.c
--- pth-orig/pth_sched.c Sat Jan 1 20:15:06 2005
+++ pth/pth_sched.c Thu Jan 13 14:05:13 2005
@@ -540,6 +540,17 @@
}
}
}
+ /* Semaphore */
+ else if (ev->ev_type == PTH_EVENT_SEM) {
+ if (ev->ev_args.SEM.sem->sem_state & PTH_SEM_INITIALIZED) {
+ if (ev->ev_args.SEM.sem->sem_count >= ev->ev_args.SEM.count)
+ {
+ this_occurred = TRUE;
+ if(ev->ev_goal & PTH_UNTIL_DECREMENT)
+ ev->ev_args.SEM.sem->sem_count -= ev->ev_args.SEM.count;
+ }
+ }
+ }
/* Thread Termination */
else if (ev->ev_type == PTH_EVENT_TID) {
if ( ( ev->ev_args.TID.tid == NULL
diff -urN pth-orig/pth_sync.c pth/pth_sync.c
--- pth-orig/pth_sync.c Sat Jan 1 20:15:06 2005
+++ pth/pth_sync.c Thu Jan 13 13:54:49 2005
@@ -380,3 +380,84 @@
return rv;
}
+/*
+** Semaphore Variables
+*/
+
+int pth_sem_init(pth_sem_t *sem)
+{
+ if (sem == NULL)
+ return pth_error(FALSE, EINVAL);
+ sem->sem_state = PTH_SEM_INITIALIZED;
+ sem->sem_count = 0;
+ return TRUE;
+}
+
+int pth_sem_dec(pth_sem_t * sem)
+{
+ return pth_sem_dec_value(sem, 1);
+}
+
+int pth_sem_dec_value(pth_sem_t * sem, unsigned count)
+{
+ static pth_key_t ev_key = PTH_KEY_INIT;
+ pth_event_t ev;
+ /* consistency checks */
+ if (sem == NULL)
+ return pth_error(FALSE, EINVAL);
+ if (!(sem->sem_state & PTH_SEM_INITIALIZED))
+ return pth_error(FALSE, EDEADLK);
+
+ ev = pth_event(PTH_EVENT_SEM|PTH_UNTIL_DECREMENT|PTH_UNTIL_COUNT|PTH_MODE_STATIC, &ev_key, sem,count);
+ pth_wait(ev);
+ return TRUE;
+}
+
+int pth_sem_inc(pth_sem_t * sem, int yield)
+{
+ return pth_sem_inc_value(sem, 1, yield);
+}
+
+int pth_sem_inc_value(pth_sem_t * sem, unsigned count, int yield)
+{
+ /* consistency checks */
+ if (sem == NULL)
+ return pth_error(FALSE, EINVAL);
+ if (!(sem->sem_state & PTH_SEM_INITIALIZED))
+ return pth_error(FALSE, EDEADLK);
+
+ sem->sem_count+=count;
+ if(yield)
+ pth_yield(NULL);
+ return TRUE;
+}
+
+int pth_sem_set_value(pth_sem_t * sem, unsigned value)
+{
+ /* consistency checks */
+ if (sem == NULL)
+ return pth_error(FALSE, EINVAL);
+ if (!(sem->sem_state & PTH_SEM_INITIALIZED))
+ return pth_error(FALSE, EDEADLK);
+
+ sem->sem_count = value;
+ return TRUE;
+}
+
+int pth_sem_get_value(pth_sem_t * sem, unsigned *value)
+{
+ /* consistency checks */
+ if (sem == NULL)
+ return pth_error(FALSE, EINVAL);
+ if (!(sem->sem_state & PTH_SEM_INITIALIZED))
+ return pth_error(FALSE, EDEADLK);
+
+ if (value == NULL)
+ return pth_error(FALSE, EINVAL);
+
+ *value = sem->sem_count;
+ return TRUE;
+}
+
+
+