ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/vendor/libpeak-0.1.2/peak/task_mutex.c
Revision: 3251
Committed: Wed Apr 2 16:58:30 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 2799 byte(s)
Log Message:
- Imported libpeak-0.1.2

File Contents

# Content
1 /* PEAK Library
2 *
3 * Copyright (c) 2003
4 * Stephane Thiell <mbuna@bugged.org>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30 #define RCSID "$Id: task_mutex.c,v 1.1.1.1 2003/12/30 02:29:31 mbuna Exp $"
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <peak/task.h>
37
38 #include "task_private.h"
39
40 #include <assert.h>
41 #include <pthread.h>
42
43
44 struct __peak_task_mutex
45 {
46 PEAK_STRUCT_RT_HEADER;
47 peak_task _task; /* Owner task */
48 pthread_mutex_t _mutex;
49 };
50
51 static void __peak_task_mutex_init(peak_task_mutex mutex, va_list vp,
52 void *ctcx);
53 static void __peak_task_mutex_finalize(peak_task_mutex mutex);
54
55 PEAK_CLASS_BASE_DECLARE(task_mutex);
56
57
58 peak_task_mutex
59 peak_task_mutex_create(peak_task task)
60 {
61 return PEAK_CLASS_CONSTRUCT1(task_mutex, task);
62 }
63
64 static void
65 __peak_task_mutex_init(peak_task_mutex mutex, va_list vp, void *ctcx)
66 {
67 mutex->_task = va_arg(vp, peak_task);
68 mutex->_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
69 }
70
71 static void
72 __peak_task_mutex_finalize(peak_task_mutex mutex)
73 {
74 pthread_mutex_destroy(&mutex->_mutex);
75 }
76
77 void
78 peak_task_mutex_lock(peak_task_mutex mutex)
79 {
80 if (_peak_is_threaded)
81 pthread_mutex_lock(&mutex->_mutex);
82 }
83
84 void
85 peak_task_mutex_trylock(peak_task_mutex mutex)
86 {
87 if (_peak_is_threaded)
88 pthread_mutex_trylock(&mutex->_mutex);
89 }
90
91 void
92 peak_task_mutex_unlock(peak_task_mutex mutex)
93 {
94 if (_peak_is_threaded)
95 pthread_mutex_unlock(&mutex->_mutex);
96 }