blob: 13a61a4c84194c3374080cbf03d881d3cd6af40d [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
Vikas Aroraa2415722012-08-09 16:18:58 -07008// -----------------------------------------------------------------------------
9//
10// Multi-threaded worker
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#ifndef WEBP_UTILS_THREAD_H_
15#define WEBP_UTILS_THREAD_H_
16
Vikas Arora1e7bf882013-03-13 16:43:18 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
Vikas Aroraa2415722012-08-09 16:18:58 -070021#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25#if WEBP_USE_THREAD
26
27#if defined(_WIN32)
28
29#include <windows.h>
30typedef HANDLE pthread_t;
31typedef CRITICAL_SECTION pthread_mutex_t;
32typedef struct {
33 HANDLE waiting_sem_;
34 HANDLE received_sem_;
35 HANDLE signal_event_;
36} pthread_cond_t;
37
38#else
39
40#include <pthread.h>
41
42#endif /* _WIN32 */
43#endif /* WEBP_USE_THREAD */
44
45// State of the worker thread object
46typedef enum {
47 NOT_OK = 0, // object is unusable
48 OK, // ready to work
49 WORK // busy finishing the current task
50} WebPWorkerStatus;
51
52// Function to be called by the worker thread. Takes two opaque pointers as
53// arguments (data1 and data2), and should return false in case of error.
54typedef int (*WebPWorkerHook)(void*, void*);
55
56// Synchronize object used to launch job in the worker thread
57typedef struct {
58#if WEBP_USE_THREAD
59 pthread_mutex_t mutex_;
60 pthread_cond_t condition_;
61 pthread_t thread_;
62#endif
63 WebPWorkerStatus status_;
64 WebPWorkerHook hook; // hook to call
65 void* data1; // first argument passed to 'hook'
66 void* data2; // second argument passed to 'hook'
67 int had_error; // return value of the last call to 'hook'
68} WebPWorker;
69
70// Must be called first, before any other method.
71void WebPWorkerInit(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070072// Must be called to initialize the object and spawn the thread. Re-entrant.
Vikas Aroraa2415722012-08-09 16:18:58 -070073// Will potentially launch the thread. Returns false in case of error.
74int WebPWorkerReset(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070075// Makes sure the previous work is finished. Returns true if worker->had_error
76// was not set and no error condition was triggered by the working thread.
Vikas Aroraa2415722012-08-09 16:18:58 -070077int WebPWorkerSync(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070078// Triggers the thread to call hook() with data1 and data2 argument. These
Vikas Aroraa2415722012-08-09 16:18:58 -070079// hook/data1/data2 can be changed at any time before calling this function,
80// but not be changed afterward until the next call to WebPWorkerSync().
81void WebPWorkerLaunch(WebPWorker* const worker);
82// Kill the thread and terminate the object. To use the object again, one
83// must call WebPWorkerReset() again.
84void WebPWorkerEnd(WebPWorker* const worker);
85
86//------------------------------------------------------------------------------
87
88#if defined(__cplusplus) || defined(c_plusplus)
89} // extern "C"
90#endif
91
92#endif /* WEBP_UTILS_THREAD_H_ */