virthread.h 5.9 KB
Newer Older
1
/*
2
 * virthread.h: basic thread synchronization primitives
3
 *
4
 * Copyright (C) 2009-2011, 2013-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21
 *
 */

22
#pragma once
23

24 25
#include "internal.h"
#include "virerror.h"
26

27
#include <pthread.h>
28

29 30 31
typedef struct virMutex virMutex;
typedef virMutex *virMutexPtr;

32 33 34 35
struct virMutex {
    pthread_mutex_t lock;
};

36 37 38
typedef struct virRWLock virRWLock;
typedef virRWLock *virRWLockPtr;

39 40 41 42 43
struct virRWLock {
    pthread_rwlock_t lock;
};


44 45 46
typedef struct virCond virCond;
typedef virCond *virCondPtr;

47 48 49 50
struct virCond {
    pthread_cond_t cond;
};

51 52 53
typedef struct virThreadLocal virThreadLocal;
typedef virThreadLocal *virThreadLocalPtr;

54 55 56 57
struct virThreadLocal {
    pthread_key_t key;
};

58 59 60
typedef struct virThread virThread;
typedef virThread *virThreadPtr;

61 62 63 64
struct virThread {
    pthread_t thread;
};

65 66 67
typedef struct virOnceControl virOnceControl;
typedef virOnceControl *virOnceControlPtr;

68 69 70 71 72
struct virOnceControl {
    pthread_once_t once;
};


73
#define VIR_MUTEX_INITIALIZER \
74
    { \
75 76 77
        .lock = PTHREAD_MUTEX_INITIALIZER \
    }

78
#define VIR_ONCE_CONTROL_INITIALIZER \
79 80
    { \
        .once = PTHREAD_ONCE_INIT \
81 82
    }

83
typedef void (*virOnceFunc)(void);
84

85 86
typedef void (*virThreadFunc)(void *opaque);

87
#define virThreadCreate(thread, joinable, func, opaque) \
J
Jiri Denemark 已提交
88 89 90 91 92
    virThreadCreateFull(thread, joinable, func, #func, false, opaque)

int virThreadCreateFull(virThreadPtr thread,
                        bool joinable,
                        virThreadFunc func,
93
                        const char *name,
J
Jiri Denemark 已提交
94
                        bool worker,
95
                        void *opaque) G_GNUC_WARN_UNUSED_RESULT;
96 97 98
void virThreadSelf(virThreadPtr thread);
bool virThreadIsSelf(virThreadPtr thread);
void virThreadJoin(virThreadPtr thread);
99

100 101
size_t virThreadMaxName(void);

102 103 104 105
/* This API is *NOT* for general use. It exists solely as a stub
 * for integration with libselinux AVC callbacks */
void virThreadCancel(virThreadPtr thread);

106 107 108 109
/* These next two functions are for debugging only, since they are not
 * guaranteed to give unique values for distinct threads on all
 * architectures, nor are the two functions guaranteed to give the same
 * value for the same thread. */
110 111
unsigned long long virThreadSelfID(void);
unsigned long long virThreadID(virThreadPtr thread);
112

113 114 115 116 117 118 119 120 121 122 123 124 125
/* Static initialization of mutexes is not possible, so we instead
 * provide for guaranteed one-time initialization via a callback
 * function.  Usage:
 * static virOnceControl once = VIR_ONCE_CONTROL_INITIALIZER;
 * static void initializer(void) { ... }
 * void myfunc()
 * {
 *     if (virOnce(&once, initializer) < 0)
 *         goto error;
 *     ...now guaranteed that initializer has completed exactly once
 * }
 */
int virOnce(virOnceControlPtr once, virOnceFunc init)
126
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
127

128 129
int virMutexInit(virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT;
int virMutexInitRecursive(virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT;
130 131 132 133 134 135
void virMutexDestroy(virMutexPtr m);

void virMutexLock(virMutexPtr m);
void virMutexUnlock(virMutexPtr m);


136
int virRWLockInit(virRWLockPtr m) G_GNUC_WARN_UNUSED_RESULT;
137 138 139 140 141 142
void virRWLockDestroy(virRWLockPtr m);

void virRWLockRead(virRWLockPtr m);
void virRWLockWrite(virRWLockPtr m);
void virRWLockUnlock(virRWLockPtr m);

143

144
int virCondInit(virCondPtr c) G_GNUC_WARN_UNUSED_RESULT;
145
int virCondDestroy(virCondPtr c);
146

147 148 149 150 151
/* virCondWait, virCondWaitUntil:
 * These functions can return without the associated predicate
 * changing value. Therefore in nearly all cases they
 * should be enclosed in a while loop that checks the predicate.
 */
152 153
int virCondWait(virCondPtr c, virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT;
int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) G_GNUC_WARN_UNUSED_RESULT;
154

155 156 157 158 159 160
void virCondSignal(virCondPtr c);
void virCondBroadcast(virCondPtr c);


typedef void (*virThreadLocalCleanup)(void *);
int virThreadLocalInit(virThreadLocalPtr l,
161
                       virThreadLocalCleanup c) G_GNUC_WARN_UNUSED_RESULT;
162
void *virThreadLocalGet(virThreadLocalPtr l);
163
int virThreadLocalSet(virThreadLocalPtr l, void*) G_GNUC_WARN_UNUSED_RESULT;
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

/**
 * VIR_ONCE_GLOBAL_INIT:
 * classname: base classname
 *
 * This macro simplifies the setup of a one-time only
 * global file initializer.
 *
 * Assuming a class called "virMyObject", and a method
 * implemented like:
 *
 *  int virMyObjectOnceInit(void) {
 *      ...do init tasks...
 *  }
 *
 * Then invoking the macro:
 *
182
 *  VIR_ONCE_GLOBAL_INIT(virMyObject);
183 184 185 186 187 188 189 190
 *
 * Will create a method
 *
 *  int virMyObjectInitialize(void);
 *
 * Which will ensure that 'virMyObjectOnceInit' is
 * guaranteed to be invoked exactly once.
 */
191
#define VIR_ONCE_GLOBAL_INIT(classname) \
192
    static virOnceControl classname ## OnceControl = VIR_ONCE_CONTROL_INITIALIZER; \
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    static virErrorPtr classname ## OnceError; \
 \
    static void classname ## Once(void) \
    { \
        if (classname ## OnceInit() < 0) \
            classname ## OnceError = virSaveLastError(); \
    } \
 \
    static int classname ## Initialize(void) \
    { \
        if (virOnce(&classname ## OnceControl, classname ## Once) < 0) \
            return -1; \
 \
        if (classname ## OnceError) { \
            virSetError(classname ## OnceError); \
            return -1; \
        } \
 \
        return 0; \
212 213
    } \
    struct classname ## EatSemicolon