File: oh_lock.h

package info (click to toggle)
openhpi 2.14.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,368 kB
  • ctags: 13,803
  • sloc: ansic: 187,087; cpp: 32,188; sh: 10,292; makefile: 4,466; perl: 1,529
file content (80 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (4)
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
/*      -*- linux-c -*-
 *
 * Copyright (c) 2003 by Intel Corp.
 * (C) Copyright IBM Corp. 2003, 2006
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Authors:
 *     Louis Zhuang <louis.zhuang@linux.intel.com>
 *     Renier Morales <renier@openhpi.org>
 */

#ifndef __OH_LOCK_H
#define __OH_LOCK_H

#include <config.h>

extern int oh_will_block;

int data_access_block_times(void);

#ifdef OH_DBG_MSGS
/*
#define dbg_lock(format, ...) \
        do { \
                if (oh_get_global_bool(OPENHPI_DEBUG_LOCK)) { \
                        fprintf(stderr, "        LOCK: %s:%d:%s: ", __FILE__, __LINE__, __func__); \
                        fprintf(stderr, format "\n", ## __VA_ARGS__); \
                } \
        } while(0)
*/
#define dbg_lock(format, ...) \
        do { \
                if (getenv("OPENHPI_DEBUG_LOCK") && !strcmp("YES",getenv("OPENHPI_DEBUG_LOCK"))) { \
                        fprintf(stderr, "        LOCK: %s:%d:%s: ", __FILE__, __LINE__, __func__); \
                        fprintf(stderr, format "\n", ## __VA_ARGS__); \
                } \
        } while(0)
#else
#define dbg_lock(format, ...)
#endif
		 
/* multi-threading support, use Posix mutex for data access */
/* initialize mutex used for data locking */
#include <glib.h>
extern GStaticRecMutex oh_main_lock;
extern int lockcount;

#define data_access_lock_init()

#define data_access_lock()                                              \
        do {                                                            \
                dbg_lock("%p - Attempting lock",  g_thread_self());     \
                if (!g_static_rec_mutex_trylock(&oh_main_lock)) {       \
                        dbg_lock("%p - Lockcount: %d",  g_thread_self(), lockcount); \
                        dbg_lock("%p - Going to block for a lock now",  g_thread_self()); \
                        oh_will_block++;                                \
                        g_static_rec_mutex_lock(&oh_main_lock);         \
                        dbg_lock("%p - Got the lock after blocking",  g_thread_self()); \
                        lockcount++;                                    \
                } else {                                                \
                        dbg_lock("%p - Got the lock because no one had it",  g_thread_self()); \
                        lockcount++;                                    \
                        dbg_lock("%p - Lockcount: %d", g_thread_self(), lockcount); \
                }                                                       \
        } while(0)

#define data_access_unlock()                              \
        do {                                              \
                lockcount--;                              \
                g_static_rec_mutex_unlock(&oh_main_lock); \
                dbg_lock("%p - released the lock",  g_thread_self());      \
        } while(0)

#endif /* __OH_LOCK_H */