Skip to content

Commit 4efb039

Browse files
almost working semaphore implementation, no tests added
1 parent 003f32a commit 4efb039

File tree

5 files changed

+93
-38
lines changed

5 files changed

+93
-38
lines changed

libports/pth-2.0.7/Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ LOBJS = pth_debug.lo pth_ring.lo pth_pqueue.lo pth_time.lo pth_errno.lo pth_mctx
103103
HSRCS = $(S)pth_compat.c $(S)pth_debug.c $(S)pth_syscall.c $(S)pth_errno.c $(S)pth_ring.c $(S)pth_mctx.c \
104104
$(S)pth_uctx.c $(S)pth_clean.c $(S)pth_time.c $(S)pth_tcb.c $(S)pth_util.c $(S)pth_pqueue.c $(S)pth_event.c \
105105
$(S)pth_sched.c $(S)pth_data.c $(S)pth_msg.c $(S)pth_cancel.c $(S)pth_sync.c $(S)pth_attr.c $(S)pth_lib.c \
106-
$(S)pth_fork.c $(S)pth_high.c $(S)pth_ext.c $(S)pth_string.c $(S)pthread.c
106+
$(S)pth_fork.c $(S)pth_high.c $(S)pth_ext.c $(S)pth_string.c $(S)pthread.c $(S)semaphore.c
107107

108108
##
109109
## ____ UTILITY DEFINITIONS _________________________________________
@@ -171,8 +171,8 @@ pth_p.h: $(S)pth_p.h.in
171171
libpth.la: $(LOBJS)
172172
$(LIBTOOL) --mode=link --quiet $(CC) -o libpth.la $(LOBJS) \
173173
-rpath $(libdir) -version-info `$(SHTOOL) version -lc -dlibtool $(_VERSION_FILE)`
174-
libpthread.la: pthread.lo $(LOBJS)
175-
$(LIBTOOL) --mode=link --quiet $(CC) -o libpthread.la pthread.lo $(LOBJS) \
174+
libpthread.la: pthread.lo semaphore.lo $(LOBJS)
175+
$(LIBTOOL) --mode=link --quiet $(CC) -o libpthread.la pthread.lo semaphore.lo $(LOBJS) \
176176
-rpath $(libdir) -version-info `$(SHTOOL) version -lc -dlibtool $(_VERSION_FILE)`
177177

178178
# build the manual pages
@@ -217,7 +217,7 @@ test_pthread: test_pthread.o test_common.o libpthread.la
217217

218218
# install the package
219219
install: all-for-install
220-
@$(MAKE) $(MKFLAGS) install-dirs install-pth install-pthread
220+
@$(MAKE) $(MKFLAGS) install-dirs install-pth install-pthread install-semaphore
221221
@touch .done-install >/dev/null 2>&1 || $(TRUE)
222222
@$(MAKE) $(MKFLAGS) what-next
223223
install-dirs:
@@ -248,10 +248,12 @@ install-pthread:
248248
$(SHTOOL) install -c -m 644 $(S)pthread.3 $(DESTDIR)$(mandir)/man3/pthread.3
249249
@umask 022; $(LIBTOOL) --mode=install \
250250
$(SHTOOL) install -c libpthread.la $(DESTDIR)$(libdir)/libpthread.la
251+
install-semaphore:
252+
$(SHTOOL) install -c -m 644 semaphore.h $(DESTDIR)$(includedir)/semaphore.h
251253

252254
# uninstall the package
253255
uninstall:
254-
@$(MAKE) $(MKFLAGS) uninstall-pthread uninstall-pth uninstall-dirs
256+
@$(MAKE) $(MKFLAGS) uninstall-pthread uninstall-semaphore uninstall-pth uninstall-dirs
255257
@touch .done-uninstall >/dev/null 2>&1 || $(TRUE)
256258
@$(MAKE) $(MKFLAGS) what-next
257259
uninstall-pthread:
@@ -260,6 +262,8 @@ uninstall-pthread:
260262
@$(RM) $(DESTDIR)$(includedir)/pthread.h
261263
@$(RM) $(DESTDIR)$(mandir)/man3/pthread.3
262264
@$(LIBTOOL) --mode=uninstall $(RM) $(DESTDIR)$(libdir)/libpthread.la
265+
uninstall-semaphore:
266+
@$(RM) $(DESTDIR)$(includedir)/semaphore.h
263267
uninstall-pth:
264268
@$(RM) $(DESTDIR)$(bindir)/pth-config
265269
@$(RM) $(DESTDIR)$(mandir)/man1/pth-config.1
@@ -457,6 +461,7 @@ pth_time.lo: pth_time.c pth_p.h pth_vers.c pth.h pth_acdef.h pth_acmac.h
457461
pth_util.lo: pth_util.c pth_p.h pth_vers.c pth.h pth_acdef.h pth_acmac.h
458462
pth_vers.lo: pth_vers.c pth_vers.c
459463
pthread.o: pthread.c pthread.h pth_p.h pth_vers.c pth.h pth_acdef.h pth_acmac.h
464+
semaphore.o: semaphore.c semaphore.h pth_p.h
460465
test_common.o: test_common.c pth.h test_common.h
461466
test_httpd.o: test_httpd.c pth.h test_common.h
462467
test_misc.o: test_misc.c pth.h

libports/pth-2.0.7/pth.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ extern int pth_sem_init(pth_sem_t *);
524524
extern int pth_sem_dec(pth_sem_t *);
525525
extern int pth_sem_dec_value(pth_sem_t *, unsigned);
526526
extern int pth_sem_inc(pth_sem_t *, int);
527-
extern int pth_sem_inc_value(pth_sem_t *, unsigned, int);
528-
extern int pth_sem_set_value(pth_sem_t *, unsigned);
529-
extern int pth_sem_get_value(pth_sem_t *, unsigned *);
527+
extern int pth_sem_inc_value(pth_sem_t *, unsigned int, int);
528+
extern int pth_sem_set_value(pth_sem_t *, unsigned int);
529+
extern int pth_sem_get_value(pth_sem_t *, unsigned int*);
530530

531531
/* user-space context functions */
532532
extern int pth_uctx_create(pth_uctx_t *);

libports/pth-2.0.7/pth_sync.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ int pth_sem_dec(pth_sem_t * sem)
401401
return pth_sem_dec_value(sem, 1);
402402
}
403403

404-
int pth_sem_dec_value(pth_sem_t * sem, unsigned count)
404+
int pth_sem_dec_value(pth_sem_t * sem, unsigned int count)
405405
{
406406
static pth_key_t ev_key = PTH_KEY_INIT;
407407
pth_event_t ev;
@@ -421,7 +421,7 @@ int pth_sem_inc(pth_sem_t * sem, int yield)
421421
return pth_sem_inc_value(sem, 1, yield);
422422
}
423423

424-
int pth_sem_inc_value(pth_sem_t * sem, unsigned count, int yield)
424+
int pth_sem_inc_value(pth_sem_t * sem, unsigned int count, int yield)
425425
{
426426
/* consistency checks */
427427
if (sem == NULL)
@@ -435,7 +435,7 @@ int pth_sem_inc_value(pth_sem_t * sem, unsigned count, int yield)
435435
return TRUE;
436436
}
437437

438-
int pth_sem_set_value(pth_sem_t * sem, unsigned value)
438+
int pth_sem_set_value(pth_sem_t * sem, unsigned int value)
439439
{
440440
/* consistency checks */
441441
if (sem == NULL)
@@ -447,7 +447,7 @@ int pth_sem_set_value(pth_sem_t * sem, unsigned value)
447447
return TRUE;
448448
}
449449

450-
int pth_sem_get_value(pth_sem_t * sem, unsigned *value)
450+
int pth_sem_get_value(pth_sem_t * sem, unsigned int*value)
451451
{
452452
/* consistency checks */
453453
if (sem == NULL)

libports/pth-2.0.7/semaphore.c

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,45 @@
3131
#include "semaphore.h"
3232
#include "pth_p.h"
3333

34+
static pthread_t s_foo_semaphore_thread;
35+
36+
static void *foo_thread_semaphore_func(void *a)
37+
{
38+
(void)a;
39+
return NULL;
40+
}
41+
3442

3543
/* Initialize semaphore object SEM to VALUE. If PSHARED then share it
3644
with other processes. */
3745
int sem_init (sem_t *sem, int pshared, unsigned int value){
38-
int ret = pth_sem_init(sem);
39-
if (ret==0)
40-
ret = pth_sem_set_value(sem, value);
46+
/*as pthread, semaphore implementation expect that any thread was
47+
created previously therefore we check it and if not created then
48+
do it now*/
49+
if (pth_current == NULL && s_foo_semaphore_thread == NULL ){
50+
pthread_create(&s_foo_semaphore_thread, NULL, foo_thread_semaphore_func, NULL);
51+
}
52+
53+
*sem = malloc(sizeof(pth_sem_t));
54+
if ( pth_sem_init(*sem) == TRUE ){
55+
if ( pth_sem_set_value(*sem, value) == FALSE ){
56+
errno=EINVAL;
57+
return -1;
58+
}
59+
}
4160
/*pshared not relevant for single process single threaded
4261
environment*/
4362
(void)pshared;
44-
return ret;
63+
return 0;
4564
}
4665

4766
/* Free resources associated with semaphore object SEM. */
4867
int sem_destroy (sem_t *sem){
49-
if (sem != NULL) return 0;
68+
if (*sem != NULL){
69+
free(*sem);
70+
*sem = NULL;
71+
return 0;
72+
}
5073
else{
5174
errno = EINVAL;
5275
return -1;
@@ -80,44 +103,68 @@ int sem_unlink (const char *name){
80103

81104
/* Wait for SEM being posted.*/
82105
int sem_wait (sem_t *sem){
83-
return pth_sem_dec(sem);
106+
if ( pth_sem_dec(*sem) == TRUE )
107+
return 0;
108+
else
109+
return -1;
84110
}
85111

86-
#ifdef __USE_XOPEN2K
87112
/* Similar to `sem_wait' but wait only until ABSTIME.*/
88113
int sem_timedwait (sem_t *sem, const struct timespec *abstime){
89-
errno=ENOSYS;
90-
return -1;
114+
unsigned int value;
115+
if ( pth_sem_get_value(*sem, &value) == TRUE ){
116+
if (value>0){
117+
return pth_sem_dec(*sem) == TRUE? 0 : -1;
118+
}
119+
else{
120+
struct timespec rem_unused;
121+
nanosleep(abstime, &rem_unused);
122+
if ( pth_sem_get_value(*sem, &value) == TRUE ){
123+
if (value>0){
124+
return pth_sem_dec(*sem) == TRUE? 0 : -1;
125+
}
126+
else{
127+
errno=ETIMEDOUT;
128+
return -1;
129+
}
130+
}
131+
}
132+
}
133+
return 0;
91134
}
92-
#endif
93135

94136
/* Test whether SEM is posted. */
95137
int sem_trywait (sem_t *sem){
96138
int ret;
97-
unsigned val;
98-
ret = pth_sem_get_value(sem, &val);
99-
if (ret==0){
100-
/*if can be decremented then do it*/
101-
if (val>0){
102-
return pth_sem_dec(sem);
139+
unsigned value;
140+
if ( pth_sem_get_value(*sem, &value) == TRUE ){
141+
/*if can be decremented then do it*/
142+
if (value>0){
143+
return pth_sem_dec(*sem) == TRUE? 0 : -1;
144+
}
145+
else{
146+
errno=EAGAIN;
147+
return -1;
103148
}
104-
ret=-1;
105-
errno=EAGAIN;
106149
}
107-
return ret;
150+
return 0;
108151
}
109152

110153
/* Post SEM. */
111154
int sem_post (sem_t *sem){
112-
return pth_sem_inc(sem, 1);
155+
if ( pth_sem_inc(*sem, 1) == TRUE )
156+
return 0;
157+
else
158+
return -1;
113159
}
114160

115161
/* Get current value of SEM and store it in *SVAL. */
116162
int sem_getvalue (sem_t *sem, int *sval){
117-
unsigned value;
118-
int ret = pth_sem_get_value(sem, value);
119-
if (ret==0)
163+
unsigned int value;
164+
if ( pth_sem_get_value(*sem, &value) == TRUE ){
120165
*sval = value;
121-
return ret;
166+
return 0;
167+
}
168+
else return -1;
122169
}
123170

libports/pth-2.0.7/semaphore.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@
3333
#define _SEMAPHORE_H
3434

3535
/* Value returned if `sem_open' failed. */
36-
#define SEM_FAILED ((pth_sem_t *) 0)
36+
#define SEM_FAILED ((sem_t *) 0)
3737

38+
/*define stubs*/
39+
#define NAMED_SEMAPHORE_ENABLE
3840
/*
3941
Macro NAMED_SEMAPHORE_ENABLE
4042
to enable stubbed implementation of named semaphores*/
4143

4244
struct timespec;
45+
struct pth_sem_st;
4346

44-
typedef pth_sem_t sem_t;
47+
typedef struct pth_sem_st *sem_t;
4548

4649
/*
4750
**

0 commit comments

Comments
 (0)