xref: /haiku/src/servers/print/ResourceManager.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 /*****************************************************************************/
2 // ResourceManager
3 //
4 // Author
5 //   Michael Pfeiffer
6 //
7 // This application and all source files used in its construction, except
8 // where noted, are licensed under the MIT License, and have been written
9 // and are:
10 //
11 // Copyright (c) 2002 OpenBeOS Project
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
31 
32 #include "ResourceManager.h"
33 
34 #include <Debug.h>
35 #include <Autolock.h>
36 
37 Resource::Resource(const char* transport, const char* address, const char* connection)
38 	: fTransport(transport)
39 	, fTransportAddress(address)
40 	, fConnection(connection)
41 	, fResourceAvailable(0)
42 
43 {
44 	if (NeedsLocking()) {
45 		fResourceAvailable = create_sem(1, "resource");
46 	}
47 }
48 
49 Resource::~Resource() {
50 	if (fResourceAvailable > 0) delete_sem(fResourceAvailable);
51 }
52 
53 bool Resource::NeedsLocking() {
54 	return !(fTransport == "Print to File" || fTransport == "NONE");
55 }
56 
57 bool Resource::Equals(const char* transport, const char* address, const char* connection) {
58 	return fTransport == transport &&
59 			fTransportAddress == address &&
60 			fConnection == connection;
61 }
62 
63 bool Resource::Lock() {
64 	if (fResourceAvailable > 0) {
65 		return acquire_sem(fResourceAvailable) == B_NO_ERROR;
66 	}
67 	return true;
68 }
69 
70 void Resource::Unlock() {
71 	if (fResourceAvailable > 0) {
72 		release_sem(fResourceAvailable);
73 	}
74 }
75 
76 ResourceManager::~ResourceManager() {
77 	ASSERT(fResources.CountItems() == 0);
78 }
79 
80 Resource* ResourceManager::Find(const char* transport, const char* address, const char* connection) {
81 	for (int i = 0; i < fResources.CountItems(); i ++) {
82 		Resource* r = fResources.ItemAt(i);
83 		if (r->Equals(transport, address, connection)) return r;
84 	}
85 	return NULL;
86 }
87 
88 Resource* ResourceManager::Allocate(const char* transport, const char* address, const char* connection) {
89 	Resource* r = Find(transport, address, connection);
90 	if (r == NULL) {
91 		r = new Resource(transport, address, connection);
92 		fResources.AddItem(r);
93 	} else {
94 		r->Acquire();
95 	}
96 	return r;
97 }
98 
99 
100 void ResourceManager::Free(Resource* r) {
101 	if (r->Release()) {
102 		fResources.RemoveItem(r);
103 	}
104 }
105