Allow 2 threads
[libcds.git] / src / topology_hpux.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <cds/os/topology.h>
32
33 #if CDS_OS_TYPE == CDS_OS_HPUX
34
35 #include <cds/algo/atomic.h>
36 #include <limits>
37
38 namespace cds { namespace OS { CDS_CXX11_INLINE_NAMESPACE namespace Hpux {
39
40     size_t topology::s_nProcMapSize = 0;
41     topology::processor_map * topology::s_procMap = nullptr;
42
43     void topology::make_processor_map()
44     {
45         spu_t nProc;
46         spu_t nMaxProcNo;
47
48         // Processor numbers are not sequential in HP-UX.
49
50         // Calculate max processor number
51         nProc = nMaxProcNo = ::mpctl( MPC_GETFIRSTSPU, 0, 0 );
52
53         while ( (nProc = ::mpctl( MPC_GETNEXTSPU, nProc, 0 )) != -1 ) {
54             if ( nMaxProcNo < nProc )
55                 nMaxProcNo = nProc;
56         }
57
58         // Allocate processor map array
59         s_nProcMapSize = nMaxProcNo + 1;
60
61         // We cannot use operator new or std::allocator in this code
62         // since the initialization phase may be called from
63         // our overloaded operator new that based on cds::mihcael::Heap
64         // As a result, a deadlock may be occured
65         s_procMap = reinterpret_cast<processor_map *>(::malloc( sizeof(s_procMap[0]) * s_nProcMapSize ));
66         processor_map * pEnd = s_procMap + s_nProcMapSize;
67
68         for ( processor_map * p = s_procMap; p != pEnd; ++p ) {
69             p->nCell = 0;
70             p->nNativeProcNo = -1;
71             p->nProcNo = std::numeric_limits<unsigned int>::max();
72         }
73
74         // Fill processor map array
75         unsigned int nProcNo = 0;
76         nProc = ::mpctl( MPC_GETFIRSTSPU, 0, 0 );
77         s_procMap[ nProc ].nNativeProcNo = nProc;
78         s_procMap[ nProc ].nProcNo = nProcNo++;
79         s_procMap[ nProc ].nCell = ::mpctl( MPC_SPUTOLDOM, nProc, 0 );
80
81         while ( (nProc = ::mpctl( MPC_GETNEXTSPU, nProc, 0 )) != -1 ) {
82             processor_map * p = s_procMap + nProc;
83             p->nNativeProcNo = nProc;
84             p->nProcNo = nProcNo++;
85             p->nCell = ::mpctl( MPC_SPUTOLDOM, nProc, 0 );
86         }
87     }
88
89     void topology::init()
90     {
91         assert( s_procMap == nullptr );
92         make_processor_map();
93     }
94
95     void topology::fini()
96     {
97         assert( s_procMap );
98         if ( s_procMap ) {
99             ::free( s_procMap );
100
101             s_procMap = nullptr;
102         }
103     }
104
105 }}} // namespace cds::OS::Hpux
106
107 #endif  // #if CDS_OS_TYPE == CDS_OS_HPUX