summaryrefslogtreecommitdiff
path: root/lib/work.c
blob: 3311023ead388dad371111850cef3ffa6b384b45 (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
/*
 * Copyright 2015 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs <bskeggs@redhat.com>
 */
#include "priv.h"

struct nvos_work {
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	pthread_t thread;
	bool done;

	void (*func)(void *);
	void *priv;
};

static void *
nvos_work(void *data)
{
	struct nvos_work *work = data;

	do {
		pthread_mutex_lock(&work->mutex);
		while (!work->priv && !work->done)
			pthread_cond_wait(&work->cond, &work->mutex);
		if (work->priv) {
			work->func(work->priv);
			work->priv = NULL;
		}
		pthread_mutex_unlock(&work->mutex);
	} while (!work->done);

	return NULL;
}

void
nvos_work_fini(struct nvos_work **pwork)
{
	struct nvos_work *work = *pwork;
	if (work) {
		pthread_mutex_lock(&work->mutex);
		work->done = true;
		pthread_cond_signal(&work->cond);
		pthread_mutex_unlock(&work->mutex);

		pthread_join(work->thread, NULL);
		pthread_cond_destroy(&work->cond);
		pthread_mutex_destroy(&work->mutex);
		free(*pwork);
		*pwork = NULL;
	}
}

bool
nvos_work_init(void (*func)(void *), void *priv, struct nvos_work **pwork)
{
	struct nvos_work *work = *pwork;

	if (unlikely(work == NULL)) {
		if (!(work = calloc(1, sizeof(*work))))
			return false;
		work->func = func;
		work->priv = NULL;
		work->done = false;

		if (pthread_mutex_init(&work->mutex, NULL))
			goto fail_mutex;
		if (pthread_cond_init(&work->cond, NULL))
			goto fail_cond;
		if (pthread_create(&work->thread, NULL, nvos_work, work))
			goto fail_thread;
		*pwork = work;
	}

	pthread_mutex_lock(&work->mutex);
	work->priv = priv;
	pthread_cond_signal(&work->cond);
	pthread_mutex_unlock(&work->mutex);
	return true;

fail_thread:
	pthread_cond_destroy(&work->cond);
fail_cond:
	pthread_mutex_destroy(&work->mutex);
fail_mutex:
	free(work);
	return false;
}