xref: /haiku/src/kits/package/solver/Solver.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 
9 
10 #include <package/solver/Solver.h>
11 
12 #include <dlfcn.h>
13 #include <pthread.h>
14 
15 
16 namespace BPackageKit {
17 
18 
19 typedef BSolver* CreateSolverFunction();
20 static CreateSolverFunction* sCreateSolver = NULL;
21 
22 static pthread_once_t sLoadLibsolvSolverAddOnInitOnce = PTHREAD_ONCE_INIT;
23 
24 
25 static void
26 load_libsolv_solver_add_on()
27 {
28 	void* imageHandle = dlopen("libpackage-add-on-libsolv.so", 0);
29 	if (imageHandle == NULL)
30 		return;
31 
32 	sCreateSolver = (CreateSolverFunction*)dlsym(imageHandle, "create_solver");
33 	if (sCreateSolver == NULL)
34 		dlclose(imageHandle);
35 }
36 
37 
38 BSolver::BSolver()
39 {
40 }
41 
42 
43 BSolver::~BSolver()
44 {
45 }
46 
47 
48 /*static*/ status_t
49 BSolver::Create(BSolver*& _solver)
50 {
51 	pthread_once(&sLoadLibsolvSolverAddOnInitOnce, &load_libsolv_solver_add_on);
52 	if (sCreateSolver == NULL)
53 		return B_NOT_SUPPORTED;
54 
55 	BSolver* solver = sCreateSolver();
56 	if (solver == NULL)
57 		return B_NO_MEMORY;
58 
59 	status_t error = solver->Init();
60 	if (error != B_OK) {
61 		delete solver;
62 		return error;
63 	}
64 
65 	_solver = solver;
66 	return B_OK;
67 }
68 
69 
70 }	// namespace BPackageKit
71