1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "oneapi/tbb/detail/_assert.h"
18 
19 #include "rml_tbb.h"
20 #include "dynamic_link.h"
21 
22 namespace tbb {
23 namespace detail {
24 namespace r1 {
25 namespace rml {
26 
27 #define MAKE_SERVER(x) DLD(__TBB_make_rml_server,x)
28 #define GET_INFO(x) DLD(__TBB_call_with_my_server_info,x)
29 #define SERVER tbb_server
30 #define CLIENT tbb_client
31 #define FACTORY tbb_factory
32 
33 #if __TBB_WEAK_SYMBOLS_PRESENT
34     #pragma weak __TBB_make_rml_server
35     #pragma weak __TBB_call_with_my_server_info
36     extern "C" {
37         ::rml::factory::status_type __TBB_make_rml_server( rml::tbb_factory& f, rml::tbb_server*& server, rml::tbb_client& client );
38         void __TBB_call_with_my_server_info( ::rml::server_info_callback_t cb, void* arg );
39     }
40 #endif /* __TBB_WEAK_SYMBOLS_PRESENT */
41 
42 #if TBB_USE_DEBUG
43 #define DEBUG_SUFFIX "_debug"
44 #else
45 #define DEBUG_SUFFIX
46 #endif /* TBB_USE_DEBUG */
47 
48 // RML_SERVER_NAME is the name of the RML server library.
49 #if _WIN32 || _WIN64
50 #define RML_SERVER_NAME "irml" DEBUG_SUFFIX ".dll"
51 #elif __APPLE__
52 #define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".dylib"
53 #elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __DragonFly__
54 #define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".so"
55 #elif __unix__
56 #define RML_SERVER_NAME "libirml" DEBUG_SUFFIX ".so.1"
57 #else
58 #error Unknown OS
59 #endif
60 
61 const ::rml::versioned_object::version_type CLIENT_VERSION = 2;
62 
63 #if __TBB_WEAK_SYMBOLS_PRESENT
64     #pragma weak __RML_open_factory
65     #pragma weak __RML_close_factory
66     extern "C" {
67         ::rml::factory::status_type __RML_open_factory ( ::rml::factory&, ::rml::versioned_object::version_type&, ::rml::versioned_object::version_type );
68         void __RML_close_factory( ::rml::factory& f );
69     }
70 #endif /* __TBB_WEAK_SYMBOLS_PRESENT */
71 
open()72 ::rml::factory::status_type FACTORY::open() {
73     // Failure of following assertion indicates that factory is already open, or not zero-inited.
74     __TBB_ASSERT_EX( !library_handle, NULL );
75     status_type (*open_factory_routine)( factory&, version_type&, version_type );
76     dynamic_link_descriptor server_link_table[4] = {
77         DLD(__RML_open_factory,open_factory_routine),
78         MAKE_SERVER(my_make_server_routine),
79         DLD(__RML_close_factory,my_wait_to_close_routine),
80         GET_INFO(my_call_with_server_info_routine),
81     };
82     status_type result;
83     if ( dynamic_link( RML_SERVER_NAME, server_link_table, 4, &library_handle ) ) {
84         version_type server_version;
85         result = (*open_factory_routine)( *this, server_version, CLIENT_VERSION );
86         // server_version can be checked here for incompatibility if necessary.
87     } else {
88         library_handle = NULL;
89         result = st_not_found;
90     }
91     return result;
92 }
93 
close()94 void FACTORY::close() {
95     if ( library_handle )
96         (*my_wait_to_close_routine)(*this);
97     if ( (size_t)library_handle>FACTORY::c_dont_unload ) {
98         dynamic_unlink(library_handle);
99         library_handle = NULL;
100     }
101 }
102 
make_server(SERVER * & s,CLIENT & c)103 ::rml::factory::status_type FACTORY::make_server( SERVER*& s, CLIENT& c) {
104     // Failure of following assertion means that factory was not successfully opened.
105     __TBB_ASSERT_EX( my_make_server_routine, NULL );
106     return (*my_make_server_routine)(*this,s,c);
107 }
108 
109 } // namespace rml
110 } // namespace r1
111 } // namespace detail
112 } // namespace tbb
113