Skip to content

Commit 3227b96

Browse files
committed
Use rwlock in ConvertibleConfig
1 parent 31a48ed commit 3227b96

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
---
44

55
## new
6-
- Add documentation
6+
- Use rwlock in ConvertibleConfig
7+
- Merged pull requests
8+
- [simplify optional.kj_value](https://github.com/kakaopensource/KakaJSON/pull/16)
9+
- [replace symbol ~= with pattern keyword](https://github.com/kakaopensource/KakaJSON/pull/17)
710

811
## [1.0.0](https://github.com/kakaopensource/KakaJSON/releases/tag/1.0.0) (2019-08-23)
912
- First public release

KakaJSON/Convert/ConvertibleConfig.swift

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public typealias JSONValueConfig = (Any?, Property) -> Any?
1515

1616
/// Config for convertible
1717
public class ConvertibleConfig {
18-
private static let lock = DispatchSemaphore(value: 1)
18+
private static var lock = pthread_rwlock_t()
19+
private static let initLock = {
20+
pthread_rwlock_init(&lock, nil)
21+
}()
22+
1923
private class Item {
2024
var modelKey: ModelKeyConfig?
2125
var modelValue: ModelValueConfig?
@@ -33,8 +37,10 @@ public class ConvertibleConfig {
3337

3438
/// get global config for modelKey
3539
public static func modelKey(from property: Property) -> ModelPropertyKey {
36-
lock.wait()
37-
defer { lock.signal() }
40+
let _ = initLock
41+
pthread_rwlock_rdlock(&lock)
42+
defer { pthread_rwlock_unlock(&lock) }
43+
3844
guard let fn = global.modelKey else { return property.name }
3945
return fn(property)
4046
}
@@ -48,8 +54,9 @@ public class ConvertibleConfig {
4854
/// get type's config for modelKey
4955
public static func modelKey(from property: Property,
5056
_ type: Convertible.Type) -> ModelPropertyKey {
51-
lock.wait()
52-
defer { lock.signal() }
57+
let _ = initLock
58+
pthread_rwlock_rdlock(&lock)
59+
defer { pthread_rwlock_unlock(&lock) }
5360

5461
if let fn = items[typeKey(type)]?.modelKey {
5562
return fn(property)
@@ -72,8 +79,10 @@ public class ConvertibleConfig {
7279
/// get global config for modelValue
7380
public static func modelValue(from jsonValue: Any?,
7481
_ property: Property) -> Any? {
75-
lock.wait()
76-
defer { lock.signal() }
82+
let _ = initLock
83+
pthread_rwlock_rdlock(&lock)
84+
defer { pthread_rwlock_unlock(&lock) }
85+
7786
guard let fn = global.modelValue else { return jsonValue }
7887
return fn(jsonValue, property)
7988
}
@@ -89,8 +98,9 @@ public class ConvertibleConfig {
8998
public static func modelValue(from jsonValue: Any?,
9099
_ property: Property,
91100
_ type: Convertible.Type) -> Any? {
92-
lock.wait()
93-
defer { lock.signal() }
101+
let _ = initLock
102+
pthread_rwlock_rdlock(&lock)
103+
defer { pthread_rwlock_unlock(&lock) }
94104

95105
if let fn = items[typeKey(type)]?.modelValue {
96106
return fn(jsonValue, property)
@@ -112,8 +122,9 @@ public class ConvertibleConfig {
112122

113123
/// get global config for JSONKey
114124
public static func JSONKey(from property: Property) -> JSONPropertyKey {
115-
lock.wait()
116-
defer { lock.signal() }
125+
let _ = initLock
126+
pthread_rwlock_rdlock(&lock)
127+
defer { pthread_rwlock_unlock(&lock) }
117128

118129
guard let fn = global.jsonKey else { return property.name }
119130
return fn(property)
@@ -128,8 +139,9 @@ public class ConvertibleConfig {
128139
/// get type's config for JSONKey
129140
public static func JSONKey(from property: Property,
130141
_ type: Convertible.Type) -> JSONPropertyKey {
131-
lock.wait()
132-
defer { lock.signal() }
142+
let _ = initLock
143+
pthread_rwlock_rdlock(&lock)
144+
defer { pthread_rwlock_unlock(&lock) }
133145

134146
if let fn = items[typeKey(type)]?.jsonKey {
135147
return fn(property)
@@ -152,8 +164,9 @@ public class ConvertibleConfig {
152164
/// get global config for JSONValue
153165
public static func JSONValue(from modelValue: Any?,
154166
_ property: Property) -> Any? {
155-
lock.wait()
156-
defer { lock.signal() }
167+
let _ = initLock
168+
pthread_rwlock_rdlock(&lock)
169+
defer { pthread_rwlock_unlock(&lock) }
157170

158171
guard let fn = global.modelValue else { return modelValue }
159172
return fn(modelValue, property)
@@ -170,8 +183,9 @@ public class ConvertibleConfig {
170183
public static func JSONValue(from modelValue: Any?,
171184
_ property: Property,
172185
_ type: Convertible.Type) -> Any? {
173-
lock.wait()
174-
defer { lock.signal() }
186+
let _ = initLock
187+
pthread_rwlock_rdlock(&lock)
188+
defer { pthread_rwlock_unlock(&lock) }
175189

176190
if let fn = items[typeKey(type)]?.jsonValue {
177191
return fn(modelValue, property)
@@ -200,8 +214,9 @@ public class ConvertibleConfig {
200214
/// set types's config for modelKey
201215
public static func setModelKey(for types: [Convertible.Type] = [],
202216
_ modelKey: @escaping ModelKeyConfig) {
203-
lock.wait()
204-
defer { lock.signal() }
217+
let _ = initLock
218+
pthread_rwlock_wrlock(&lock)
219+
defer { pthread_rwlock_unlock(&lock) }
205220

206221
// clear model key cache
207222
Metadata.modelTypes.forEach { $0.clearModelKeys() }
@@ -230,8 +245,9 @@ public class ConvertibleConfig {
230245
/// set types's config for modelValue
231246
public static func setModelValue(for types: [Convertible.Type] = [],
232247
modelValue: @escaping ModelValueConfig) {
233-
lock.wait()
234-
defer { lock.signal() }
248+
let _ = initLock
249+
pthread_rwlock_wrlock(&lock)
250+
defer { pthread_rwlock_unlock(&lock) }
235251

236252
if types.count == 0 {
237253
global.modelValue = modelValue
@@ -257,8 +273,9 @@ public class ConvertibleConfig {
257273
/// set types's config for jsonKey
258274
public static func setJSONKey(for types: [Convertible.Type] = [],
259275
jsonKey: @escaping JSONKeyConfig) {
260-
lock.wait()
261-
defer { lock.signal() }
276+
let _ = initLock
277+
pthread_rwlock_wrlock(&lock)
278+
defer { pthread_rwlock_unlock(&lock) }
262279

263280
// clear JSON key cache
264281
Metadata.modelTypes.forEach { $0.clearJSONKeys() }
@@ -287,8 +304,9 @@ public class ConvertibleConfig {
287304
/// set types's config for jsonValue
288305
public static func setJSONValue(for types: [Convertible.Type] = [],
289306
jsonValue: @escaping JSONValueConfig) {
290-
lock.wait()
291-
defer { lock.signal() }
307+
let _ = initLock
308+
pthread_rwlock_wrlock(&lock)
309+
defer { pthread_rwlock_unlock(&lock) }
292310

293311
if types.count == 0 {
294312
global.jsonValue = jsonValue

KakaJSON/Convert/Values.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import CoreGraphics
1414
public struct Values {
1515
static func value(_ val: Any?, _ type: Any.Type) -> Any? {
1616
guard let v = val.kj_value else { return nil }
17-
if v is NSNull { return v }
18-
1917
if Swift.type(of: v) == type { return v }
18+
if v is NSNull { return nil }
2019

2120
switch type {
2221
case is NumberValue.Type: return _number(v, type)

KakaJSONDemo.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@
323323
2D906860230F7B7D00243BB6 /* Convert */ = {
324324
isa = PBXGroup;
325325
children = (
326-
2D906861230F7B7D00243BB6 /* Logger.swift */,
327-
2D906862230F7B7D00243BB6 /* ConvertibleEnum.swift */,
328326
2D906863230F7B7D00243BB6 /* Convertible.swift */,
329-
2D906864230F7B7D00243BB6 /* KJ.swift */,
330327
2D906865230F7B7D00243BB6 /* ConvertibleConfig.swift */,
328+
2D906862230F7B7D00243BB6 /* ConvertibleEnum.swift */,
329+
2D906861230F7B7D00243BB6 /* Logger.swift */,
330+
2D906864230F7B7D00243BB6 /* KJ.swift */,
331331
2D906866230F7B7D00243BB6 /* TypeProxy.swift */,
332332
2D46A2CA230FCD370043F5BA /* Values.swift */,
333333
);

KakaJSONDemo.xcodeproj/xcshareddata/xcschemes/KakaJSONDemo.xcscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</BuildActionEntries>
2424
</BuildAction>
2525
<TestAction
26-
buildConfiguration = "Debug"
26+
buildConfiguration = "Release"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
@@ -52,7 +52,7 @@
5252
</AdditionalOptions>
5353
</TestAction>
5454
<LaunchAction
55-
buildConfiguration = "Release"
55+
buildConfiguration = "Debug"
5656
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5757
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
5858
launchStyle = "0"

0 commit comments

Comments
 (0)