Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * xcam_thread.h - Thread |
| 3 | * |
| 4 | * Copyright (c) 2014 Intel Corporation |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Wind Yuan <feng.yuan@intel.com> |
| 19 | */ |
| 20 | |
| 21 | #ifndef XCAM_THREAD_H |
| 22 | #define XCAM_THREAD_H |
| 23 | |
Wind Yuan | 9fbfce6 | 2017-11-24 19:24:15 +0800 | [diff] [blame] | 24 | #include <xcam_std.h> |
| 25 | #include <xcam_mutex.h> |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 26 | |
| 27 | namespace XCam { |
| 28 | |
| 29 | class Thread { |
| 30 | public: |
| 31 | Thread (const char *name = NULL); |
| 32 | virtual ~Thread (); |
| 33 | |
| 34 | bool start (); |
Wind Yuan | dd9d137 | 2015-12-17 02:07:43 +0800 | [diff] [blame] | 35 | virtual bool emit_stop (); |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 36 | bool stop (); |
| 37 | bool is_running (); |
Wind Yuan | 4f52875 | 2017-09-01 17:09:00 +0800 | [diff] [blame] | 38 | const char *get_name () const { |
| 39 | return _name; |
| 40 | } |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | // return true to start loop, else the thread stopped |
| 44 | virtual bool started (); |
| 45 | virtual void stopped (); |
| 46 | // return true to continue; false to stop |
| 47 | virtual bool loop () = 0; |
| 48 | private: |
| 49 | XCAM_DEAD_COPY (Thread); |
| 50 | |
| 51 | |
| 52 | private: |
| 53 | static int thread_func (void *user_data); |
| 54 | |
| 55 | private: |
| 56 | char *_name; |
| 57 | pthread_t _thread_id; |
| 58 | XCam::Mutex _mutex; |
| 59 | XCam::Cond _exit_cond; |
| 60 | bool _started; |
Wind Yuan | b7f6991 | 2017-02-13 01:46:41 -0500 | [diff] [blame] | 61 | bool _stopped; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | #endif //XCAM_THREAD_H |