blob: 9afe09679f766d6c18ce7004c4bf9474a82f172a [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// This code is licensed under the same terms as WebM:
4// Software License Agreement: http://www.webmproject.org/license/software/
5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// Multi-threaded worker
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#ifndef WEBP_UTILS_THREAD_H_
13#define WEBP_UTILS_THREAD_H_
14
Vikas Arora1e7bf882013-03-13 16:43:18 -070015#ifdef HAVE_CONFIG_H
16#include "config.h"
17#endif
18
Vikas Aroraa2415722012-08-09 16:18:58 -070019#if defined(__cplusplus) || defined(c_plusplus)
20extern "C" {
21#endif
22
23#if WEBP_USE_THREAD
24
25#if defined(_WIN32)
26
27#include <windows.h>
28typedef HANDLE pthread_t;
29typedef CRITICAL_SECTION pthread_mutex_t;
30typedef struct {
31 HANDLE waiting_sem_;
32 HANDLE received_sem_;
33 HANDLE signal_event_;
34} pthread_cond_t;
35
36#else
37
38#include <pthread.h>
39
40#endif /* _WIN32 */
41#endif /* WEBP_USE_THREAD */
42
43// State of the worker thread object
44typedef enum {
45 NOT_OK = 0, // object is unusable
46 OK, // ready to work
47 WORK // busy finishing the current task
48} WebPWorkerStatus;
49
50// Function to be called by the worker thread. Takes two opaque pointers as
51// arguments (data1 and data2), and should return false in case of error.
52typedef int (*WebPWorkerHook)(void*, void*);
53
54// Synchronize object used to launch job in the worker thread
55typedef struct {
56#if WEBP_USE_THREAD
57 pthread_mutex_t mutex_;
58 pthread_cond_t condition_;
59 pthread_t thread_;
60#endif
61 WebPWorkerStatus status_;
62 WebPWorkerHook hook; // hook to call
63 void* data1; // first argument passed to 'hook'
64 void* data2; // second argument passed to 'hook'
65 int had_error; // return value of the last call to 'hook'
66} WebPWorker;
67
68// Must be called first, before any other method.
69void WebPWorkerInit(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070070// Must be called to initialize the object and spawn the thread. Re-entrant.
Vikas Aroraa2415722012-08-09 16:18:58 -070071// Will potentially launch the thread. Returns false in case of error.
72int WebPWorkerReset(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070073// Makes sure the previous work is finished. Returns true if worker->had_error
74// was not set and no error condition was triggered by the working thread.
Vikas Aroraa2415722012-08-09 16:18:58 -070075int WebPWorkerSync(WebPWorker* const worker);
Vikas Arora1e7bf882013-03-13 16:43:18 -070076// Triggers the thread to call hook() with data1 and data2 argument. These
Vikas Aroraa2415722012-08-09 16:18:58 -070077// hook/data1/data2 can be changed at any time before calling this function,
78// but not be changed afterward until the next call to WebPWorkerSync().
79void WebPWorkerLaunch(WebPWorker* const worker);
80// Kill the thread and terminate the object. To use the object again, one
81// must call WebPWorkerReset() again.
82void WebPWorkerEnd(WebPWorker* const worker);
83
84//------------------------------------------------------------------------------
85
86#if defined(__cplusplus) || defined(c_plusplus)
87} // extern "C"
88#endif
89
90#endif /* WEBP_UTILS_THREAD_H_ */