/proc/self/cwd/external/nsync/internal/wait.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2016 Google Inc. |
2 | | |
3 | | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | you may not use this file except in compliance with the License. |
5 | | You may obtain a copy of the License at |
6 | | |
7 | | http://www.apache.org/licenses/LICENSE-2.0 |
8 | | |
9 | | Unless required by applicable law or agreed to in writing, software |
10 | | distributed under the License is distributed on an "AS IS" BASIS, |
11 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | See the License for the specific language governing permissions and |
13 | | limitations under the License. */ |
14 | | |
15 | | #include "nsync_cpp.h" |
16 | | #include "platform.h" |
17 | | #include "compiler.h" |
18 | | #include "cputype.h" |
19 | | #include "nsync.h" |
20 | | #include "sem.h" |
21 | | #include "dll.h" |
22 | | #include "wait_internal.h" |
23 | | #include "common.h" |
24 | | #include "atomic.h" |
25 | | |
26 | | NSYNC_CPP_START_ |
27 | | |
28 | | int nsync_wait_n (void *mu, void (*lock) (void *), void (*unlock) (void *), |
29 | | nsync_time abs_deadline, |
30 | 0 | int count, struct nsync_waitable_s *waitable[]) { |
31 | 0 | int ready; |
32 | 0 | IGNORE_RACES_START (); |
33 | 0 | for (ready = 0; ready != count && |
34 | 0 | nsync_time_cmp ((*waitable[ready]->funcs->ready_time) ( |
35 | 0 | waitable[ready]->v, NULL), |
36 | 0 | nsync_time_zero) > 0; |
37 | 0 | ready++) { |
38 | 0 | } |
39 | 0 | if (ready == count && nsync_time_cmp (abs_deadline, nsync_time_zero) > 0) { |
40 | 0 | int i; |
41 | 0 | int unlocked = 0; |
42 | 0 | int j; |
43 | 0 | int enqueued = 1; |
44 | 0 | waiter *w = nsync_waiter_new_ (); |
45 | 0 | struct nsync_waiter_s nw_set[4]; |
46 | 0 | struct nsync_waiter_s *nw = nw_set; |
47 | 0 | if (count > (int) (sizeof (nw_set) / sizeof (nw_set[0]))) { |
48 | 0 | nw = (struct nsync_waiter_s *) malloc (count * sizeof (nw[0])); |
49 | 0 | } |
50 | 0 | for (i = 0; i != count && enqueued; i++) { |
51 | 0 | nw[i].tag = NSYNC_WAITER_TAG; |
52 | 0 | nw[i].sem = &w->sem; |
53 | 0 | nsync_dll_init_ (&nw[i].q, &nw[i]); |
54 | 0 | ATM_STORE (&nw[i].waiting, 0); |
55 | 0 | nw[i].flags = 0; |
56 | 0 | enqueued = (*waitable[i]->funcs->enqueue) (waitable[i]->v, &nw[i]); |
57 | 0 | } |
58 | |
|
59 | 0 | if (i == count) { |
60 | 0 | nsync_time min_ntime; |
61 | 0 | if (mu != NULL) { |
62 | 0 | (*unlock) (mu); |
63 | 0 | unlocked = 1; |
64 | 0 | } |
65 | 0 | do { |
66 | 0 | min_ntime = abs_deadline; |
67 | 0 | for (j = 0; j != count; j++) { |
68 | 0 | nsync_time ntime; |
69 | 0 | ntime = (*waitable[j]->funcs->ready_time) ( |
70 | 0 | waitable[j]->v, &nw[j]); |
71 | 0 | if (nsync_time_cmp (ntime, min_ntime) < 0) { |
72 | 0 | min_ntime = ntime; |
73 | 0 | } |
74 | 0 | } |
75 | 0 | } while (nsync_time_cmp (min_ntime, nsync_time_zero) > 0 && |
76 | 0 | nsync_mu_semaphore_p_with_deadline (&w->sem, |
77 | 0 | min_ntime) == 0); |
78 | 0 | } |
79 | | |
80 | | /* An attempt was made above to enqueue waitable[0..i-1]. |
81 | | Dequeue any that are still enqueued, and remember the index |
82 | | of the first ready (i.e., not still enqueued) object, if any. */ |
83 | 0 | for (j = 0; j != i; j++) { |
84 | 0 | int was_still_enqueued = |
85 | 0 | (*waitable[j]->funcs->dequeue) (waitable[j]->v, &nw[j]); |
86 | 0 | if (!was_still_enqueued && ready == count) { |
87 | 0 | ready = j; |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | 0 | if (nw != nw_set) { |
92 | 0 | free (nw); |
93 | 0 | } |
94 | 0 | nsync_waiter_free_ (w); |
95 | 0 | if (unlocked) { |
96 | 0 | (*lock) (mu); |
97 | 0 | } |
98 | 0 | } |
99 | 0 | IGNORE_RACES_END (); |
100 | 0 | return (ready); |
101 | 0 | } |
102 | | |
103 | | NSYNC_CPP_END_ |