1 //--------------------------------------------------------------------------
2 // Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3 // Copyright (C) 2013-2013 Sourcefire, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License Version 2 as published
7 // by the Free Software Foundation.  You may not use, modify or distribute
8 // this program under any other version of the GNU General Public License.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //--------------------------------------------------------------------------
19 
20 #ifndef THREAD_H
21 #define THREAD_H
22 
23 // basic thread management utilities
24 
25 #include <string>
26 
27 #include "main/snort_types.h"
28 
29 #define THREAD_LOCAL_TBD
30 //#define THREAD_LOCAL // for single-threaded debugging
31 
32 // `__thread` is a gnu extension that at present is slightly faster than
33 // `thread_local` (possibly due to the lack of dynamic initialization)
34 #ifdef USE_THREAD_LOCAL
35 #    define THREAD_LOCAL thread_local
36 #else
37 #    define THREAD_LOCAL __thread
38 #endif
39 
40 enum SThreadType
41 {
42     STHREAD_TYPE_OTHER,
43     STHREAD_TYPE_PACKET,
44     STHREAD_TYPE_MAIN
45 };
46 
47 void set_instance_id(unsigned);
48 void set_thread_type(SThreadType);
49 
50 void set_run_num(uint16_t);
51 uint16_t get_run_num();
52 
53 namespace snort
54 {
55 SO_PUBLIC unsigned get_instance_id();
56 SO_PUBLIC SThreadType get_thread_type();
57 
in_main_thread()58 SO_PUBLIC inline bool in_main_thread()
59 { return get_thread_type() == STHREAD_TYPE_MAIN; }
60 
is_packet_thread()61 SO_PUBLIC inline bool is_packet_thread()
62 { return get_thread_type() == STHREAD_TYPE_PACKET; }
63 
64 // all modules that use packet thread files should call this function to
65 // get a packet thread specific path.  name should be the module name or
66 // derived therefrom.
67 SO_PUBLIC const char* get_instance_file(std::string&, const char* name);
68 }
69 
70 #endif
71