Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_BASE_TASK_ANNOTATIONS_H_ 7 : #define SRC_BASE_TASK_ANNOTATIONS_H_ 8 : 9 : #include <stdlib.h> 10 : #include <set> 11 : #include <string> 12 : 13 : #include <boost/scoped_ptr.hpp> 14 : 15 : #include "base/task.h" 16 : class ConcurrencyChecker { 17 : public: 18 : static bool enable_; 19 : ConcurrencyChecker(); 20 : ConcurrencyChecker(const char *task_ids[], size_t count); 21 : void Check(); 22 : void CheckIfMainThr(); 23 457 : static bool IsInMainThr() { return (Task::Running() == NULL); } 24 : private: 25 : typedef std::set<int> TaskIdSet; 26 : TaskIdSet id_set_; 27 : }; 28 : 29 : class ConcurrencyScope { 30 : public: 31 : explicit ConcurrencyScope(int task_id); 32 : explicit ConcurrencyScope(const std::string &task_id); 33 : ~ConcurrencyScope(); 34 : private: 35 : class ScopeTask; 36 : void SetRunningTask(int task_id); 37 : boost::scoped_ptr<ScopeTask> unit_test_task_; 38 : }; 39 : 40 : #define CHECK_CONCURRENCY(...) \ 41 : do { \ 42 : if (!ConcurrencyChecker::enable_) break; \ 43 : const char *_X_array[] = { __VA_ARGS__ }; \ 44 : ConcurrencyChecker checker( \ 45 : _X_array, sizeof(_X_array) / sizeof(char *)); \ 46 : checker.Check(); \ 47 : } while (0) 48 : 49 : #define CHECK_CONCURRENCY_MAIN_THR() \ 50 : do { \ 51 : if (!ConcurrencyChecker::enable_) break; \ 52 : ConcurrencyChecker checker; \ 53 : checker.CheckIfMainThr(); \ 54 : } while (0) 55 : 56 : #endif // SRC_BASE_TASK_ANNOTATIONS_H_