@@ -25,7 +25,7 @@ int release_lock(SEM_HANDLE sem) {
2525 return 1 ;
2626}
2727
28- int exists_lock (SEM_HANDLE sem ) {
28+ int exist_lock (SEM_HANDLE sem ) {
2929 int res = -1 ;
3030
3131 errno = 0 ;
@@ -64,6 +64,7 @@ puts(__func__);
6464 if (sem == SEM_FAILED ) {
6565 errno = 0 ;
6666 // Semaphore exists, just opens it.
67+ printf ("Try to open glock '%s'\n" , shm_semlock_counters .name_shm_lock );
6768 sem = sem_open (shm_semlock_counters .name_shm_lock , 0 );
6869 // Not exists, creates it.
6970 if (force_open && sem == SEM_FAILED ) {
@@ -78,50 +79,48 @@ puts(__func__);
7879 }
7980
8081 // Locks to semaphore.
81- if ( sem != SEM_FAILED && ACQUIRE_SHM_LOCK ) {
82- printf ("Shm Lock ok on %p\n" , sem );
83- // connect to Shared mem
84- shm = shm_open (shm_semlock_counters .name_shm , oflag , 0 );
85- if (shm != -1 ) {
86- shm_semlock_counters .handle_shm = shm ;
87- printf ("Shared Mem ok on '%d'\n" , shm );
88- char * ptr = (char * )mmap (NULL ,
89- size_shm ,
90- (PROT_WRITE | PROT_READ ),
91- MAP_SHARED ,
92- shm_semlock_counters .handle_shm ,
93- 0L );
94- shm_semlock_counters .header = (HeaderObject * )ptr ;
95- shm_semlock_counters .counters = (CounterObject * )(ptr + sizeof (HeaderObject ));
96- printf ("Shared memory size is %lu vs %d\n" , size_shm ,
97- shm_semlock_counters .header -> size_shm );
98- // Initialization is successful.
99- shm_semlock_counters .state_this = THIS_AVAILABLE ;
100- header = shm_semlock_counters .header ;
101- counter = shm_semlock_counters .counters ;
102- if (unlink ) {
103- atexit (delete_shm_semlock_counters );
82+ ACQUIRE_SHM_LOCK ;
83+ printf ("Shm Lock ok on %p\n" , sem );
84+ // connect to Shared mem
85+ shm = shm_open (shm_semlock_counters .name_shm , oflag , 0 );
86+ if (shm != -1 ) {
87+ shm_semlock_counters .handle_shm = shm ;
88+ printf ("Shared Mem ok on '%d'\n" , shm );
89+ char * ptr = (char * )mmap (NULL ,
90+ size_shm ,
91+ (PROT_WRITE | PROT_READ ),
92+ MAP_SHARED ,
93+ shm_semlock_counters .handle_shm ,
94+ 0L );
95+ shm_semlock_counters .header = (HeaderObject * )ptr ;
96+ shm_semlock_counters .counters = (CounterObject * )(ptr + sizeof (HeaderObject ));
97+ printf ("Shared memory size is %lu vs %d\n" , size_shm ,
98+ shm_semlock_counters .header -> size_shm );
99+ // Initialization is successful.
100+ shm_semlock_counters .state_this = THIS_AVAILABLE ;
101+ header = shm_semlock_counters .header ;
102+ counter = shm_semlock_counters .counters ;
103+ if (unlink ) {
104+ atexit (delete_shm_semlock_counters );
104105
105- } else {
106- atexit (delete_shm_semlock_counters_without_unlink );
107- }
108- puts ("Ok...." );
109106 } else {
110- printf ( "The shared memory '%s' does not exist\n" , shm_semlock_counters . name_shm );
107+ atexit ( delete_shm_semlock_counters_without_unlink );
111108 }
112- RELEASE_SHM_LOCK ;
113- printf ("Shm Unlock ok on %p\n" , sem );
109+ puts ("Ok...." );
114110 } else {
115- puts ( "No Semaphore opened !!" );
111+ printf ( "The shared memory '%s' does not exist\n" , shm_semlock_counters . name_shm );
116112 }
113+ RELEASE_SHM_LOCK ;
114+ printf ("Shm Unlock ok on %p\n" , sem );
115+
117116}
118117
119- static void _delete_shm_semlock_counters (int unlink ) {
118+ void _delete_shm_semlock_counters (int unlink ) {
120119
121120 puts ("clean up..." );
122121 if (shm_semlock_counters .state_this == THIS_AVAILABLE ) {
123122 if (shm_semlock_counters .counters ) {
124- ACQUIRE_SHM_LOCK ;
123+ ACQUIRE_SHM_LOCK ;
125124 // unmmap
126125 munmap (shm_semlock_counters .counters ,
127126 shm_semlock_counters .header -> size_shm );
@@ -134,7 +133,9 @@ static void _delete_shm_semlock_counters(int unlink) {
134133 }
135134 // close lock
136135 sem_close (shm_semlock_counters .handle_shm_lock );
137- sem_unlink (shm_semlock_counters .name_shm_lock );
136+ if (unlink ) {
137+ sem_unlink (shm_semlock_counters .name_shm_lock );
138+ }
138139}
139140
140141
0 commit comments