1 /* spin_mutex.c                  -*-C-*-
2  *
3  *************************************************************************
4  *
5  *  @copyright
6  *  Copyright (C) 2009-2013, Intel Corporation
7  *  All rights reserved.
8  *
9  *  @copyright
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *    * Redistributions of source code must retain the above copyright
15  *      notice, this list of conditions and the following disclaimer.
16  *    * Redistributions in binary form must reproduce the above copyright
17  *      notice, this list of conditions and the following disclaimer in
18  *      the documentation and/or other materials provided with the
19  *      distribution.
20  *    * Neither the name of Intel Corporation nor the names of its
21  *      contributors may be used to endorse or promote products derived
22  *      from this software without specific prior written permission.
23  *
24  *  @copyright
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35  *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  *  POSSIBILITY OF SUCH DAMAGE.
37  **************************************************************************/
38 
39 #include "spin_mutex.h"
40 #include "bug.h"
41 #include "os.h"
42 #include "stats.h"
43 
44 // TBD (11/30/12): We should be doing a conditional test-xchg instead
45 // of an unconditional xchg operation for the spin mutex.
46 
47 /* m->lock == 1 means that mutex M is locked */
48 #define TRY_ACQUIRE(m) (__cilkrts_xchg(&(m)->lock, 1) == 0)
49 
50 /* ICC 11.1+ understands release semantics and generates an
51    ordinary store with a software memory barrier. */
52 #if __ICC >= 1110
53 #define RELEASE(m) __sync_lock_release(&(m)->lock)
54 #else
55 #define RELEASE(m) __cilkrts_xchg(&(m)->lock, 0)
56 #endif
57 
58 
spin_mutex_create()59 spin_mutex* spin_mutex_create()
60 {
61     spin_mutex* mutex = (spin_mutex*)__cilkrts_malloc(sizeof(spin_mutex));
62     spin_mutex_init(mutex);
63     return mutex;
64 }
65 
spin_mutex_init(struct spin_mutex * m)66 void spin_mutex_init(struct spin_mutex *m)
67 {
68     // Use a simple assignment so Inspector doesn't bug us about the
69     // interlocked exchange doing a read of an uninitialized variable.
70     // By definition there can't be a race when we're initializing the
71     // lock...
72     m->lock = 0;
73 }
74 
spin_mutex_lock(struct spin_mutex * m)75 void spin_mutex_lock(struct spin_mutex *m)
76 {
77     int count;
78     const int maxspin = 1000; /* SWAG */
79     if (!TRY_ACQUIRE(m)) {
80         count = 0;
81         do {
82             do {
83                 __cilkrts_short_pause();
84                 if (++count >= maxspin) {
85                     /* let the OS reschedule every once in a while */
86                     __cilkrts_yield();
87                     count = 0;
88                 }
89             } while (m->lock != 0);
90         } while (!TRY_ACQUIRE(m));
91     }
92 }
93 
spin_mutex_trylock(struct spin_mutex * m)94 int spin_mutex_trylock(struct spin_mutex *m)
95 {
96     return TRY_ACQUIRE(m);
97 }
98 
spin_mutex_unlock(struct spin_mutex * m)99 void spin_mutex_unlock(struct spin_mutex *m)
100 {
101     RELEASE(m);
102 }
103 
spin_mutex_destroy(struct spin_mutex * m)104 void spin_mutex_destroy(struct spin_mutex *m)
105 {
106     __cilkrts_free(m);
107 }
108 
109 /* End spin_mutex.c */
110