Skip to content

Commit 065921b

Browse files
committed
refactor code, remove global lock and shared memory when count of semaphore is 0
1 parent be6972f commit 065921b

File tree

6 files changed

+410
-247
lines changed

6 files changed

+410
-247
lines changed

Modules/_multiprocessing/dump_shm_macosx/dump_shared_mem.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ typedef sem_t *SEM_HANDLE;
1414
// Static datas for each process.
1515
CountersWorkaround shm_semlock_counters = {
1616
.state_this = THIS_NOT_OPEN,
17-
.name_shm = "/shm_gh125828",
17+
.name_shm = SHAREDMEM_NAME,
1818
.handle_shm = (MEMORY_HANDLE)0,
19-
.create_shm = 0,
20-
.name_shm_lock = "/mp_gh125828",
19+
.name_shm_lock = GLOCK_NAME,
2120
.handle_shm_lock = (SEM_HANDLE)0,
2221
.header = (HeaderObject *)NULL,
2322
.counters = (CounterObject *)NULL,
@@ -27,10 +26,9 @@ HeaderObject *header = NULL;
2726
CounterObject *counter = NULL;
2827

2928
static char *show_counter(char *p, CounterObject *counter) {
30-
sprintf(p, "p:%p, n:%s, v:%d, u:%d, t:%s", counter,
29+
sprintf(p, "p:%p, n:%s, v:%d, t:%s", counter,
3130
counter->sem_name,
3231
counter->internal_value,
33-
counter->unlink_done,
3432
ctime(&counter->ctimestamp));
3533
return p;
3634
}
@@ -73,7 +71,7 @@ int main(int argc, char *argv[]) {
7371
puts("+++++++++");
7472
if (argc > 1) {
7573
sscanf(argv[1], "%d", &repeat);
76-
if (argc >= 2) {
74+
if (argc > 2) {
7775
puts(argv[2]);
7876
sscanf(argv[2], "%lu", &udelay);
7977
}
@@ -88,13 +86,17 @@ int main(int argc, char *argv[]) {
8886
if (shm_semlock_counters.state_this == THIS_AVAILABLE) {
8987
memset(&save, '\0', sizeof(save));
9088
do {
89+
//ACQUIRE_SHM_LOCK;
90+
acquire_lock(shm_semlock_counters.handle_shm_lock);
9191
if (memcmp(&save, shm_semlock_counters.header, sizeof(HeaderObject)) ) {
9292
time_t timestamp = time(NULL);
9393
puts(ctime(&timestamp));
9494
dump_shm_semlock_counters();
9595
memcpy(&save, shm_semlock_counters.header, sizeof(HeaderObject));
9696
puts("==========");
9797
}
98+
//RELEASE_SHM_LOCK;
99+
sem_post(shm_semlock_counters.handle_shm_lock);
98100
usleep(udelay);
99101
} while(repeat--);
100102
}

Modules/_multiprocessing/dump_shm_macosx/reset_shared_mem.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ typedef sem_t *SEM_HANDLE;
1111
// Static datas for each process.
1212
CountersWorkaround shm_semlock_counters = {
1313
.state_this = THIS_NOT_OPEN,
14-
.name_shm = "/shm_gh125828",
14+
.name_shm = SHAREDMEM_NAME,
1515
.handle_shm = (MEMORY_HANDLE)0,
16-
.create_shm = 0,
17-
.name_shm_lock = "/mp_gh125828",
16+
.name_shm_lock = GLOCK_NAME,
1817
.handle_shm_lock = (SEM_HANDLE)0,
1918
.header = (HeaderObject *)NULL,
2019
.counters = (CounterObject *)NULL,

Modules/_multiprocessing/dump_shm_macosx/shared_mem.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ int release_lock(SEM_HANDLE sem) {
2525
return 1;
2626
}
2727

28+
int exists_lock(SEM_HANDLE sem) {
29+
int res = -1 ;
30+
31+
errno = 0;
32+
res = sem_trywait(sem);
33+
if (res < 0 ) {
34+
if (errno == EBADF) {
35+
puts("global lock does not exist");
36+
shm_semlock_counters.state_this = THIS_NOT_OPEN;
37+
return 0;
38+
}
39+
return 0;
40+
}
41+
if (sem_post(sem) < 0) {
42+
return 0;
43+
}
44+
return 1;
45+
}
46+
2847
void connect_shm_semlock_counters(int unlink, int force_open, int call_release_lock) {
2948
puts(__func__);
3049

@@ -102,24 +121,24 @@ static void _delete_shm_semlock_counters(int unlink) {
102121
puts("clean up...");
103122
if (shm_semlock_counters.state_this == THIS_AVAILABLE) {
104123
if (shm_semlock_counters.counters) {
105-
if (ACQUIRE_SHM_LOCK) {
106-
// unmmap
107-
munmap(shm_semlock_counters.counters,
108-
shm_semlock_counters.header->size_shm);
109-
if (unlink) {
110-
shm_unlink(shm_semlock_counters.name_shm);
111-
}
112-
shm_semlock_counters.state_this = THIS_CLOSED;
113-
RELEASE_SHM_LOCK;
124+
ACQUIRE_SHM_LOCK;
125+
// unmmap
126+
munmap(shm_semlock_counters.counters,
127+
shm_semlock_counters.header->size_shm);
128+
if (unlink) {
129+
shm_unlink(shm_semlock_counters.name_shm);
114130
}
131+
shm_semlock_counters.state_this = THIS_CLOSED;
132+
RELEASE_SHM_LOCK;
115133
}
116-
// close lock
117-
sem_close(shm_semlock_counters.handle_shm_lock);
118-
sem_unlink(shm_semlock_counters.name_shm_lock);
119134
}
135+
// close lock
136+
sem_close(shm_semlock_counters.handle_shm_lock);
137+
sem_unlink(shm_semlock_counters.name_shm_lock);
120138
}
121139

122140

141+
123142
void delete_shm_semlock_counters_without_unlink(void) {
124143
puts(__func__);
125144
_delete_shm_semlock_counters(0);

Modules/_multiprocessing/dump_shm_macosx/shared_mem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ extern CountersWorkaround shm_semlock_counters;
55
extern HeaderObject *header;
66
extern CounterObject *counter;
77

8-
int acquire_lock(SEM_HANDLE sem);
9-
int release_lock(SEM_HANDLE sem);
8+
extern int acquire_lock(SEM_HANDLE sem);
9+
extern int release_lock(SEM_HANDLE sem);
1010

1111
void connect_shm_semlock_counters(int unlink, int force_connect, int release_lock);
1212
void delete_shm_semlock_counters_without_unlink(void);

0 commit comments

Comments
 (0)