@@ -219,7 +219,7 @@ _multiprocessing_SemLock_release_impl(SemLockObject *self)
219219/*
220220
221221On MacOSX, `sem_getvalue` is not implemented. This workaround proposes to handle
222- the internal value Semaphore (not for (R)Lock) in a shared memory available for each processed.
222+ the internal value Semaphore ((R)Lock are out of scope ) in a shared memory available for each processed.
223223This internal value is stored in a structure named CounterObject with:
224224+ the semaphore name,
225225+ the (internal) current value,
@@ -239,17 +239,17 @@ a new counter, when counter is no more used or when an extension of shared memor
239239
240240-> Structure of shared memory:
241241
242- ---- Extendable array of MAX_COUNTERS Counters ----
243- / \
244- +----------------+----------------+---/ /---+------------+ ------------+
245- | Header | Counter 1 | | CounterN -1 | CounterN |
246- |----------------|----------------| .... |------------| ------------|
247- | | | | | |
248- | nb_semlocks | sem_name | | | |
249- | max_semlocks | internal_value | | | |
250- | size_shm | n_procs | | | |
251- | | | | | |
252- +----------------+----------------+---/ /---+------------+ ------------+*/
242+ ----- Extendable array of MAX_COUNTERS Counters ----
243+ / \
244+ +----------------+----------------+---/ /---+-------------+- ------------+
245+ | Header | Counter 1 | | Counter N -1 | Counter N |
246+ |----------------|----------------| .... |-------------|- ------------|
247+ | | | | | |
248+ | nb_semlocks | sem_name | | | |
249+ | max_semlocks | internal_value | | | |
250+ | size_shm | n_procs | | | |
251+ | | | | | |
252+ +----------------+----------------+---/ /---+-------------+- ------------+*/
253253
254254// ------------- list of structs --------------
255255typedef struct {
@@ -259,9 +259,9 @@ typedef struct {
259259} HeaderObject ;
260260
261261typedef struct _co {
262- char sem_name [16 ] ; // Name of semaphore.
263- int internal_value ; // Internal value of semaphore, update on each acquire/release.
264- int n_procs ; // Number of attached processes.
262+ char sem_name [16 ] ; // Name of semaphore.
263+ short internal_value ; // Internal value of semaphore, update on each acquire/release.
264+ short n_procs ; // Number of attached processes.
265265} CounterObject ;
266266
267267/*-----------
@@ -312,7 +312,7 @@ typedef struct {
312312#define ISMINE (o ) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
313313#define ISSEMAPHORE2 (m , k ) (m > 1 && k == SEMAPHORE)
314314#define ISSEMAPHORE (o ) ((o)->maxvalue > 1 && (o)->kind == SEMAPHORE)
315- #define NO_VALUE (-1 )
315+ #define NO_VALUE (-1000 )
316316
317317
318318/*------
@@ -341,43 +341,32 @@ typedef struct {
341341#define ACQUIRE_COUNTER_MUTEX (s ) (/*fprintf(OUTPUT, "\t\t\t>>>> PID:%05d: MUTEX ACQ(%s) >>>>:%d\n", getpid(), (s)->name, __LINE__) && */ sem_wait ((s )-> handle_mutex ) >= 0 )
342342#define RELEASE_COUNTER_MUTEX (s ) (/*fprintf(OUTPUT, "\t\t\t<<<< PID:%05d: MUTEX REL(%s) <<<<:%d\n", getpid(), (s)->name, __LINE__) && */ sem_post ((s )-> handle_mutex ) >= 0 )
343343
344- #define CONNECT_MUTEX (s ) do { \
345- if (!((s )-> handle_mutex ) && (s )-> name ) { \
346- (s )-> handle_mutex = sem_open (_make_sem_name (buf , (s )-> name ), 0 ) ; \
347- } \
348- } while (0 )
349-
350- #define CONNECT_SHARED_COUNTER (s ) do { \
351- if (!((s)->counter) && ((s)->handle_mutex)) { \
352- (s)->counter = search_counter_from_sem_name(s) ; \
353- } \
354- } while(0)
355344// Global datas for each process.
356345CountersWorkaround shm_semlock_counters = {
357- .name_shm = "/shm_gh125828\0 " ,
346+ .name_shm = "/shm_gh125828" ,
358347 .handle_shm = (MEMORY_HANDLE )0 ,
359- .name_gmlock = "/mp_gh125828\0 " ,
348+ .name_gmlock = "/mp_gh125828" ,
360349 .handle_gmlock = (SEM_HANDLE )0 ,
361350 .mem_state = MEMORY_NOT_OPEN ,
362351 ._errno = 0 ,
363352 .counters = (SharedCounters * )NULL ,
364353} ;
365354
366- static char * gh_name = "_gh125828\0 " ;
355+ static char * gh_name = "_gh125828" ;
367356static char * _make_sem_name (char * buf , const char * name ) {
368357 strcpy (buf , name ) ;
369358 strcat (buf , gh_name ) ;
370359 return buf ;
371360}
372361
373-
374362char * str_counter (char * p , CounterObject * counter ){
375363 sprintf (p , "(%s, V:%d, P:%d)" , counter -> sem_name , counter -> internal_value , counter -> n_procs ) ;
376364 return p ;
377365}
378366
379367static void _dump_shm_semlock_counters (void ) {
380368 char buf [128 ] ;
369+ int i = 0 , j = 0 ;
381370 return ;
382371
383372 if (shm_semlock_counters .mem_state == MEMORY_AVAILABLE ) {
@@ -387,8 +376,7 @@ static void _dump_shm_semlock_counters(void) {
387376
388377 sprintf (buf , "Nb counters:%d/max counters:%d" , header -> nb_semlocks , header -> max_semlocks ) ;
389378 SUB_ENTER_STR (__func__ , buf ) ;
390- for (int i = 0 , j = 0 ; i < header -> max_semlocks && j < header -> nb_semlocks ;
391- i ++ , counter ++ ) {
379+ for ( ; i < header -> max_semlocks && j < header -> nb_semlocks ; i ++ , counter ++ ) {
392380 if (counter -> sem_name [0 ] != 0 ) {
393381 SUB_ENTER_STR (__func__ , str_counter (buf , counter )) ;
394382 ++ j ;
@@ -439,7 +427,7 @@ SUB_ENTER_STR(__func__, from_sem_name) ;
439427 /**/
440428 errno = 0 ;
441429 sem = sem_open (shm_semlock_counters .name_gmlock , O_CREAT , 0600 , 1 ) ;
442- printf ("name:%s, sem %p, errno %d\n" , shm_semlock_counters .name_gmlock , sem , errno ) ;
430+ // printf("name:%s, sem %p, errno %d\n", shm_semlock_counters.name_gmlock, sem, errno) ;
443431 if (sem == SEM_FAILED ) {
444432 errno = 0 ;
445433 shm_semlock_counters ._errno = errno ;
@@ -454,7 +442,7 @@ SUB_ENTER_STR(__func__, from_sem_name) ;
454442 shm_semlock_counters .handle_gmlock = sem ;
455443 char buf [256 ] ;
456444 sprintf (buf , "%s, GMLOCK %p" , action , sem ) ;
457- puts (buf ) ;
445+ // puts(buf) ;
458446 SUB_ENTER_STR (__func__ , buf ) ;
459447 // first time
460448 // Locks global lock in order to be alone to
@@ -473,7 +461,7 @@ SUB_ENTER_STR(__func__, from_sem_name) ;
473461 action = "SHM_CONNECT" ;
474462 }
475463 sprintf (buf , "%s, SHM %d" , action , shm ) ;
476- puts (buf ) ;
464+ // puts(buf) ;
477465 SUB_ENTER_STR (__func__ , buf ) ;
478466 // mmap
479467 if (res >= 0 ) {
@@ -514,7 +502,7 @@ SUB_ENTER(__func__) ;
514502 return ;
515503 }
516504
517- // global lock must be LOCKED
505+ // Global lock must be LOCKED.
518506 int nb_new_counters = (int )(MAX_COUNTERS + 1 ) ;
519507 long size = sizeof (CounterObject )* nb_new_counters ;
520508 CounterObject * new = NULL ;
@@ -536,7 +524,7 @@ SUB_ENTER(__func__) ;
536524void delete_shm_semlock_counters (void ) { // SemLockObject *self) {
537525char buf [256 ] ;
538526sprintf (buf , "delete POD:%d" , getpid ()) ;
539- puts (buf ) ;
527+ // puts(buf) ;
540528ENTER (__func__ ) ;
541529
542530 return ;
@@ -632,14 +620,13 @@ SUB_ENTER_STR(__func__, buf) ;
632620 CounterObject * counter = NULL ;
633621 HeaderObject * header = & shm_semlock_counters .counters -> header ;
634622
623+ // Does Semaphore exist in shared memory ?
635624 counter = _search_counter_from_sem_name (name ) ;
636625 if (counter ) {
637626 self -> counter = counter ;
638627 ++ counter -> n_procs ;
639- sprintf (buf , "FIND Counter %p" , counter ) ;
640- //SUB_ENTER_STR(__func__, buf) ;
641628 } else {
642- // Find an available slot
629+ // Find an available slot.
643630 counter = _search_counter_free_slot () ;
644631 if (counter == NULL ) {
645632 _extend_shm_semlock_counters () ;
@@ -648,28 +635,22 @@ SUB_ENTER_STR(__func__, buf) ;
648635 return NULL ;
649636 }
650637 }
651- //SUB_ENTER_STR(__func__, "ADD Counter") ;
652638 self -> counter = counter ;
653639 strcpy (counter -> sem_name , name ) ;
654640 counter -> n_procs = 1 ;
655641 counter -> internal_value = value ;
656642 /*
657- If unlink, self->name is NULL,
658- we need to get the semaphore name to link with
643+ If unlink is set to 1 , self->name is set to NULL,
644+ we need to save the semaphore name in order to link with
659645 its counter.
660646 */
661647 if (unlink && self -> name == NULL ) {
662648 self -> name = counter -> sem_name ;
663649 }
664650
665- sprintf (buf , "CREATE Counter %p" , counter ) ;
666- //SUB_ENTER_STR(__func__, buf) ;
667-
668651 // update header
669652 ++ header -> nb_semlocks ;
670653 }
671- puts (buf ) ;
672-
673654 return counter ;
674655}
675656
@@ -723,7 +704,7 @@ static void dealloc_counter(SemLockObject *self) {
723704char buf [128 ] ;
724705sprintf (buf , "dealloc PID:%d, s:%s, counter:%p, %d:procs" , getpid (), self -> name , self -> counter , self -> counter -> n_procs ) ;
725706if (ISSEMAPHORE (self )) {
726- puts (buf ) ;
707+ // puts(buf) ;
727708}
728709SUB_ENTER_STR (__func__ , buf ) ;
729710
@@ -749,7 +730,6 @@ SUB_ENTER_STR(__func__, buf) ;
749730 if (!counter -> n_procs ) {
750731 errno = 0 ;
751732 if (ACQUIRE_GENERAL_LOCK ) {
752- printf ("\tclear sem:%s\n" , self -> name ) ;
753733 memset (counter , 0 , sizeof (CounterObject )) ;
754734 self -> counter = NULL ;
755735 -- header -> nb_semlocks ;
@@ -771,21 +751,7 @@ SUB_ENTER_STR(__func__, buf) ;
771751 }
772752}
773753
774- /*
775- static int _on_update_counter_value(CounterObject *counter, int incr) {
776- char buf[128] ;
777- //SUB_ENTER_STR(__func__, str_counter(buf, counter)) ;
778-
779- counter->internal_value += incr ;
780- return counter->internal_value ;
781-
782- }
783- */
784-
785754static int on_get_counter_value (SemLockObject * self ) {
786- char buf [256 ] ;
787- //SUB_ENTER_STR(__func__, str_counter(buf, self->counter)) ;
788-
789755 int value = -1 ;
790756
791757 // acquire counter lock
@@ -909,7 +875,7 @@ char buf[512] ;
909875sprintf (buf , "%s, PID:%d, s:%s, Block:%d, cnt:%p" , __func__ , getpid (), self -> name , blocking , self -> counter ) ;
910876if (ISSEMAPHORE (self )) {
911877 sprintf (buf , "%s, PID:%d, s:%s, h:%p/%p b:%d, c:%p, v:%d" , "acq_enter" , getpid (), self -> name , self -> handle , self -> handle_mutex , blocking , self -> counter , self -> counter -> internal_value ) ;
912- puts (buf ) ;
878+ // puts(buf) ;
913879}
914880//ENTER_STR(__func__, buf) ;
915881
@@ -952,7 +918,7 @@ if (ISSEMAPHORE(self)) {
952918 errno = err ;
953919 if (ISSEMAPHORE (self )) {
954920 sprintf (buf , "\t%s, PID:%d, s:%s, h:%p/%p b:%d, c:%p, v:%d" , "acq_trywait" , getpid (), self -> name , self -> handle , self -> handle_mutex , blocking , self -> counter , self -> counter -> internal_value ) ;
955- puts (buf ) ;
921+ // puts(buf) ;
956922 }
957923
958924 if (res < 0 && errno == EAGAIN && blocking ) {
@@ -967,7 +933,7 @@ if (ISSEMAPHORE(self)) {
967933 }
968934 if (ISSEMAPHORE (self )) {
969935 sprintf (buf , "\t%s, PID:%d, s:%s, h:%p/%p b:%d, c:%p, v:%d" , "acq_wait" , getpid (), self -> name , self -> handle , self -> handle_mutex , blocking , self -> counter , self -> counter -> internal_value ) ;
970- puts (buf ) ;
936+ // puts(buf) ;
971937 }
972938 Py_END_ALLOW_THREADS
973939 err = errno ;
@@ -983,14 +949,14 @@ if (ISSEMAPHORE(self)) {
983949 if (errno == EINTR ) {
984950 return NULL ;
985951 }
986- puts ("*******res<0**********$" ) ;
952+ // puts("*******res<0**********$") ;
987953 return PyErr_SetFromErrno (PyExc_OSError );
988954 }
989955#ifdef HAVE_BROKEN_SEM_GETVALUE
990956 if (ISSEMAPHORE (self )) {
991957 /*
992958 sprintf(buf, "\t--\t%s, PID:%d, s:%s, res:%d, b:%d, v:%d", "acq_decr", getpid(), self->name, res, blocking, self->counter->internal_value) ;
993- puts(buf) ;
959+ // puts(buf) ;
994960 if (!self->handle_mutex) {
995961 self->handle_mutex = sem_open(_make_sem_name(buf, self->name), 0) ;
996962 }
@@ -1004,14 +970,14 @@ if (ISSEMAPHORE(self)) {
1004970 }
1005971 if (!RELEASE_COUNTER_MUTEX (self )) {
1006972 PyErr_SetFromErrno (PyExc_OSError );
1007- puts ("ERRRRRRRRRRRRRRRRRRRRROR" ) ;
973+ // puts("ERRRRRRRRRRRRRRRRRRRRROR") ;
1008974 return NULL ;
1009975 }
1010976 sprintf (buf , "\t--\t%s, PID:%d, s:%s, res:%d, b:%d, v:%d" , "acq_decr" , getpid (), self -> name , res , blocking , self -> counter -> internal_value ) ;
1011- puts (buf ) ;
977+ // puts(buf) ;
1012978 } else {
1013979 PyErr_SetFromErrno (PyExc_OSError );
1014- puts ("ERRRRRRRRRRRRRRRRRRRRROR" ) ;
980+ // puts("ERRRRRRRRRRRRRRRRRRRRROR") ;
1015981 return NULL ;
1016982 }
1017983 }
@@ -1037,7 +1003,7 @@ char buf[256] ;
10371003sprintf (buf , "%s, PID:%d, self:%s, counter:%p" ,"rel_enter" , getpid (), self -> name , self -> counter ) ;
10381004if (ISSEMAPHORE (self )) {
10391005 sprintf (buf , "%s, PID:%d, s:%s, h:%p/%p, c:%p, v:%d" , "rel_enter" , getpid (), self -> name , self -> handle , self -> handle_mutex , self -> counter , self -> counter -> internal_value ) ;
1040- puts (buf ) ;
1006+ // puts(buf) ;
10411007}
10421008//ENTER_STR(__func__, buf) ;
10431009
@@ -1091,7 +1057,7 @@ if (ISSEMAPHORE(self)) {
10911057 if (ACQUIRE_COUNTER_MUTEX (self )) {
10921058 val = self -> counter -> internal_value ;
10931059 if (!RELEASE_COUNTER_MUTEX (self )) {
1094- puts ("ERRRRRRRRRRRRORRRRR" ) ;
1060+ // puts("ERRRRRRRRRRRRORRRRR") ;
10951061 PyErr_SetFromErrno (PyExc_OSError );
10961062 return NULL ;
10971063 }
@@ -1129,14 +1095,14 @@ if (ISSEMAPHORE(self)) {
11291095 ++ self -> counter -> internal_value ;
11301096 if (!RELEASE_COUNTER_MUTEX (self )) {
11311097 PyErr_SetFromErrno (PyExc_OSError );
1132- puts ("ERRRRRRRRRRRRRRRRRRRRROR" ) ;
1098+ // puts("ERRRRRRRRRRRRRRRRRRRRROR") ;
11331099 return NULL ;
11341100 }
11351101 sprintf (buf , "\t++\t%s, PID:%d, s:%s, vincr:%d" , "rel_incr" , getpid (), self -> name , self -> counter -> internal_value ) ;
1136- puts (buf ) ;
1102+ // puts(buf) ;
11371103 } else {
11381104 PyErr_SetFromErrno (PyExc_OSError );
1139- puts ("ERRRRRRRRRRRRRRRRRRRRROR" ) ;
1105+ // puts("ERRRRRRRRRRRRRRRRRRRRROR") ;
11401106 return NULL ;
11411107 }
11421108 }
@@ -1244,7 +1210,7 @@ ENTER_STR(__func__, buf) ;
12441210 semlock -> handle_mutex = handle_mutex ;
12451211 semlock -> counter = new_counter (semlock , name , value , unlink ) ;
12461212 sprintf (buf , "%s, PID:%d, s:%s, h:%p/%p, c:%p, v:%d, p:%d" , "implr" , getpid (), semlock -> name , semlock -> handle , semlock -> handle_mutex , semlock -> counter , semlock -> counter -> internal_value , semlock -> counter -> n_procs ) ;
1247- puts (buf ) ;
1213+ // puts(buf) ;
12481214 }
12491215#endif
12501216
@@ -1339,7 +1305,7 @@ ENTER_STR(__func__, buf) ;
13391305 semlock -> handle_mutex = handle_mutex ;
13401306 semlock -> counter = connect_counter (semlock , name ) ;
13411307 sprintf (buf , "%s, PID:%d, s:%s, h:%p/%p, c:%p, v:%d, p:%d" , "rebuild" , getpid (), semlock -> name , semlock -> handle , semlock -> handle_mutex , semlock -> counter , semlock -> counter -> internal_value , semlock -> counter -> n_procs ) ;
1342- puts (buf ) ;
1308+ // puts(buf) ;
13431309 }
13441310#endif
13451311 return result ;
0 commit comments