summaryrefslogtreecommitdiff
path: root/libcilkrts/runtime/spin_mutex.h
blob: b0045ab93136558695225a48946f402de9c618e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* spin_mutex.h                  -*-C++-*-
 *
 *************************************************************************
 *
 *  @copyright
 *  Copyright (C) 2009-2013, Intel Corporation
 *  All rights reserved.
 *  
 *  @copyright
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *    * Neither the name of Intel Corporation nor the names of its
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *  
 *  @copyright
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 **************************************************************************/

/**
 * @file spin_mutex.h
 *
 * @brief Support for Cilk runtime mutexes.
 *
 * Cilk runtime mutexes are implemented as simple spin loops.
 *
 * This file is similar to a worker_mutex, except it does not have an
 * owner field.
 *
 * TBD: This class, worker_mutex, and os_mutex overlap quite a bit in
 * functionality.  Can we unify these mutexes somehow?
 */
#ifndef INCLUDED_SPIN_MUTEX_DOT_H
#define INCLUDED_SPIN_MUTEX_DOT_H

#include <cilk/common.h>
#include "rts-common.h"
#include "cilk_malloc.h"

__CILKRTS_BEGIN_EXTERN_C

/**
 * Mutexes are treated as an abstract data type within the Cilk
 * runtime system.  They are implemented as simple spin loops.
 */
typedef struct spin_mutex {
    /** Mutex spin loop variable. 0 if unowned, 1 if owned. */
    volatile int lock;

    /** Padding so the mutex takes up a cache line. */
    char pad[64/sizeof(int) - 1];
} spin_mutex;


/**
 * @brief Create a new Cilk spin_mutex.
 *
 * @return Returns an initialized spin mutex.  
 */
COMMON_PORTABLE
spin_mutex* spin_mutex_create();

/**
 * @brief Initialize a Cilk spin_mutex.
 *
 * @param m Spin_Mutex to be initialized.
 */
COMMON_PORTABLE
void spin_mutex_init(spin_mutex *m);

/**
 * @brief Acquire a Cilk spin_mutex.
 *
 * If statistics are being gathered, the time spent
 * acquiring the spin_mutex will be attributed to the specified worker.
 *
 * @param m Spin_Mutex to be initialized.
 */
COMMON_PORTABLE
void spin_mutex_lock(struct spin_mutex *m);
/**
 * @brief Attempt to lock a Cilk spin_mutex and fail if it isn't available.
 *
 * @param m Spin_Mutex to be acquired.
 *
 * @return 1 if the spin_mutex was acquired.
 * @return 0 if the spin_mutex was not acquired.
 */
COMMON_PORTABLE
int spin_mutex_trylock(struct spin_mutex *m);

/**
 * @brief Release a Cilk spin_mutex.
 *
 * @param m Spin_Mutex to be released.
 */
COMMON_PORTABLE
void spin_mutex_unlock(struct spin_mutex *m);

/**
 * @brief Deallocate a Cilk spin_mutex.  Currently does nothing.
 *
 * @param m Spin_Mutex to be deallocated.
 */
COMMON_PORTABLE
void spin_mutex_destroy(struct spin_mutex *m);

__CILKRTS_END_EXTERN_C

#endif // ! defined(INCLUDED_SPIN_MUTEX_DOT_H)