diff options
Diffstat (limited to 'src/common/helper.c')
-rw-r--r-- | src/common/helper.c | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/src/common/helper.c b/src/common/helper.c index 1307401a..826aa1ee 100644 --- a/src/common/helper.c +++ b/src/common/helper.c | |||
@@ -78,6 +78,7 @@ static gboolean saveCallWrapper(gpointer data) { | |||
78 | SaveCall * call = data; | 78 | SaveCall * call = data; |
79 | int i; | 79 | int i; |
80 | 80 | ||
81 | printf("Running save call!\n"); | ||
81 | /* clearly, we are no longer pending, | 82 | /* clearly, we are no longer pending, |
82 | so remove from psc */ | 83 | so remove from psc */ |
83 | if (call->sem != NULL) { | 84 | if (call->sem != NULL) { |
@@ -98,6 +99,7 @@ static gboolean saveCallWrapper(gpointer data) { | |||
98 | call->func(call->args); | 99 | call->func(call->args); |
99 | if (call->sem != NULL) | 100 | if (call->sem != NULL) |
100 | SEMAPHORE_UP(call->sem); | 101 | SEMAPHORE_UP(call->sem); |
102 | printf("Running save call done.\n"); | ||
101 | return FALSE; | 103 | return FALSE; |
102 | } | 104 | } |
103 | 105 | ||
@@ -132,32 +134,6 @@ void gtkSaveCall(SimpleCallback func, | |||
132 | } | 134 | } |
133 | } | 135 | } |
134 | 136 | ||
135 | int gtkRunSomeSaveCalls() { | ||
136 | int i; | ||
137 | |||
138 | if (! PTHREAD_SELF_TEST(&mainThread)) | ||
139 | return NO; | ||
140 | MUTEX_LOCK(&sclock); | ||
141 | if (pscCount == 0) { | ||
142 | MUTEX_UNLOCK(&sclock); | ||
143 | return NO; | ||
144 | } | ||
145 | i = randomi(pscCount); | ||
146 | if (TRUE == g_idle_remove_by_data(psc[i])) | ||
147 | saveCallWrapper(psc[i]); | ||
148 | MUTEX_UNLOCK(&sclock); | ||
149 | gnunet_util_sleep(50 * cronMILLIS); | ||
150 | /* sleep here is somewhat important, first of | ||
151 | all, after completion we need to give the | ||
152 | semaphore-mechanism time to remove the save-call | ||
153 | from the list to avoid running it twice; | ||
154 | also, this function might be called in a tight | ||
155 | loop (see search.c), so we should give the | ||
156 | other threads some time to run. */ | ||
157 | |||
158 | return YES; | ||
159 | } | ||
160 | |||
161 | /** | 137 | /** |
162 | * Callback for handling "delete_event": close the window | 138 | * Callback for handling "delete_event": close the window |
163 | */ | 139 | */ |
@@ -483,6 +459,7 @@ static void * shutdownCode(void * arg) { | |||
483 | 459 | ||
484 | ret = cls->realMain(cls->arg); | 460 | ret = cls->realMain(cls->arg); |
485 | SEMAPHORE_UP(cls->sig); | 461 | SEMAPHORE_UP(cls->sig); |
462 | PTHREAD_KILL(&mainThread, SIGALRM); | ||
486 | return ret; | 463 | return ret; |
487 | } | 464 | } |
488 | 465 | ||
@@ -491,6 +468,7 @@ void run_with_save_calls(PThreadMain cb, | |||
491 | PTHREAD_T doneThread; | 468 | PTHREAD_T doneThread; |
492 | void * unused; | 469 | void * unused; |
493 | struct rwsc_closure cls; | 470 | struct rwsc_closure cls; |
471 | int i; | ||
494 | 472 | ||
495 | cls.sig = SEMAPHORE_NEW(0); | 473 | cls.sig = SEMAPHORE_NEW(0); |
496 | cls.realMain = cb; | 474 | cls.realMain = cb; |
@@ -500,8 +478,22 @@ void run_with_save_calls(PThreadMain cb, | |||
500 | &cls, | 478 | &cls, |
501 | 64*1024)) | 479 | 64*1024)) |
502 | DIE_STRERROR("pthread_create"); | 480 | DIE_STRERROR("pthread_create"); |
503 | while (OK != SEMAPHORE_DOWN_NONBLOCKING(cls.sig)) | 481 | if ( (! PTHREAD_SELF_TEST(&mainThread)) || |
504 | gtkRunSomeSaveCalls(); | 482 | (pscCount == 0) ) { |
483 | /* another thread will run the save calls */ | ||
484 | SEMAPHORE_DOWN(cls.sig); | ||
485 | } else { | ||
486 | while (OK != SEMAPHORE_DOWN_NONBLOCKING(cls.sig)) { | ||
487 | MUTEX_LOCK(&sclock); | ||
488 | if (pscCount > 0) { | ||
489 | i = randomi(pscCount); | ||
490 | if (TRUE == g_idle_remove_by_data(psc[i])) | ||
491 | saveCallWrapper(psc[i]); | ||
492 | } | ||
493 | MUTEX_UNLOCK(&sclock); | ||
494 | gnunet_util_sleep(50 * cronMILLIS); | ||
495 | } | ||
496 | } | ||
505 | PTHREAD_JOIN(&doneThread, | 497 | PTHREAD_JOIN(&doneThread, |
506 | &unused); | 498 | &unused); |
507 | SEMAPHORE_FREE(cls.sig); | 499 | SEMAPHORE_FREE(cls.sig); |