virthread.h 6.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
 *
 */

#ifndef __THREADS_H_
23
# define __THREADS_H_
24

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

28 29
# include <pthread.h>

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

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

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

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


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

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

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

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

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

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

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

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


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

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

84
typedef void (*virOnceFunc)(void);
85 86 87 88

int virThreadInitialize(void) ATTRIBUTE_RETURN_CHECK;
void virThreadOnExit(void);

89 90
typedef void (*virThreadFunc)(void *opaque);

J
Jiri Denemark 已提交
91 92 93 94 95 96 97 98 99
# define virThreadCreate(thread, joinable, func, opaque) \
    virThreadCreateFull(thread, joinable, func, #func, false, opaque)

int virThreadCreateFull(virThreadPtr thread,
                        bool joinable,
                        virThreadFunc func,
                        const char *funcName,
                        bool worker,
                        void *opaque) ATTRIBUTE_RETURN_CHECK;
100 101 102
void virThreadSelf(virThreadPtr thread);
bool virThreadIsSelf(virThreadPtr thread);
void virThreadJoin(virThreadPtr thread);
103

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

108 109 110 111
/* 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. */
112 113
unsigned long long virThreadSelfID(void);
unsigned long long virThreadID(virThreadPtr thread);
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* 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)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;

130
int virMutexInit(virMutexPtr m) ATTRIBUTE_RETURN_CHECK;
S
Stefan Berger 已提交
131
int virMutexInitRecursive(virMutexPtr m) ATTRIBUTE_RETURN_CHECK;
132 133 134 135 136 137
void virMutexDestroy(virMutexPtr m);

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


138 139 140 141 142 143 144
int virRWLockInit(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK;
void virRWLockDestroy(virRWLockPtr m);

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

145 146

int virCondInit(virCondPtr c) ATTRIBUTE_RETURN_CHECK;
147
int virCondDestroy(virCondPtr c);
148

149 150 151 152 153
/* 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.
 */
154
int virCondWait(virCondPtr c, virMutexPtr m) ATTRIBUTE_RETURN_CHECK;
155
int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) ATTRIBUTE_RETURN_CHECK;
156

157 158 159 160 161 162 163 164
void virCondSignal(virCondPtr c);
void virCondBroadcast(virCondPtr c);


typedef void (*virThreadLocalCleanup)(void *);
int virThreadLocalInit(virThreadLocalPtr l,
                       virThreadLocalCleanup c) ATTRIBUTE_RETURN_CHECK;
void *virThreadLocalGet(virThreadLocalPtr l);
165
int virThreadLocalSet(virThreadLocalPtr l, void*) ATTRIBUTE_RETURN_CHECK;
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

/**
 * 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:
 *
 *  VIR_ONCE_GLOBAL_INIT(virMyObject)
 *
 * Will create a method
 *
 *  int virMyObjectInitialize(void);
 *
 * Which will ensure that 'virMyObjectOnceInit' is
 * guaranteed to be invoked exactly once.
 */
# define VIR_ONCE_GLOBAL_INIT(classname)                                \
    static virOnceControl classname ## OnceControl = VIR_ONCE_CONTROL_INITIALIZER; \
195
    static virErrorPtr classname ## OnceError;                          \
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                                                                        \
    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;                                                       \
    }

216
#endif