This source file includes following definitions.
- ompi_sync_wait_mt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include "wait_sync.h"
16
17 static opal_mutex_t wait_sync_lock = OPAL_MUTEX_STATIC_INIT;
18 static ompi_wait_sync_t* wait_sync_list = NULL;
19
20 static opal_atomic_int32_t num_thread_in_progress = 0;
21
22 #define WAIT_SYNC_PASS_OWNERSHIP(who) \
23 do { \
24 pthread_mutex_lock( &(who)->lock); \
25 pthread_cond_signal( &(who)->condition ); \
26 pthread_mutex_unlock( &(who)->lock); \
27 } while(0)
28
29 int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
30 {
31
32
33
34
35 if(sync->count <= 0)
36 return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
37
38
39 pthread_mutex_lock(&sync->lock);
40
41
42
43
44 if(sync->count <= 0) {
45 pthread_mutex_unlock(&sync->lock);
46 return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
47 }
48
49
50 OPAL_THREAD_LOCK(&wait_sync_lock);
51 if( NULL == wait_sync_list ) {
52 sync->next = sync->prev = sync;
53 wait_sync_list = sync;
54 } else {
55 sync->prev = wait_sync_list->prev;
56 sync->prev->next = sync;
57 sync->next = wait_sync_list;
58 wait_sync_list->prev = sync;
59 }
60 OPAL_THREAD_UNLOCK(&wait_sync_lock);
61
62
63
64
65
66
67 check_status:
68 if( sync != wait_sync_list && num_thread_in_progress >= opal_max_thread_in_progress) {
69 pthread_cond_wait(&sync->condition, &sync->lock);
70
71
72
73
74
75
76
77 if( sync->count <= 0 ) {
78 pthread_mutex_unlock(&sync->lock);
79 goto i_am_done;
80 }
81
82 goto check_status;
83 }
84 pthread_mutex_unlock(&sync->lock);
85
86 OPAL_THREAD_ADD_FETCH32(&num_thread_in_progress, 1);
87 while(sync->count > 0) {
88 opal_progress();
89 }
90 OPAL_THREAD_ADD_FETCH32(&num_thread_in_progress, -1);
91
92 i_am_done:
93
94 OPAL_THREAD_LOCK(&wait_sync_lock);
95 sync->prev->next = sync->next;
96 sync->next->prev = sync->prev;
97
98 if( sync == wait_sync_list ) {
99 wait_sync_list = (sync == sync->next) ? NULL : sync->next;
100 if( NULL != wait_sync_list )
101 WAIT_SYNC_PASS_OWNERSHIP(wait_sync_list);
102 }
103 OPAL_THREAD_UNLOCK(&wait_sync_lock);
104
105 return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
106 }