Skip to content

Commit c06a601

Browse files
authored
Improve documentation (#395)
* Improve documentation Explicitly document thread-safety guarantees, PeekSource behavior and FileSystem support for JS-target. Closes #392, #393, #394
1 parent 1dae118 commit c06a601

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

core/Module.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ Core IO primitives.
8383

8484
Basic API for working with files.
8585

86+
#### Thread-safety guarantees
87+
88+
Until stated otherwise, types and functions provided by the library are not thread safe.
89+
8690
#### Known issues
8791

8892
- [#312](https://github.com/Kotlin/kotlinx-io/issues/312) For `wasmWasi` target, directory listing ([kotlinx.io.files.FileSystem.list]) does not work with NodeJS runtime on Windows,

core/common/src/Buffer.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ import kotlin.jvm.JvmSynthetic
4141
* [Buffer] implements both [Source] and [Sink] and could be used as a source or a sink,
4242
* but unlike regular sinks and sources its [close], [flush], [emit], [hintEmit]
4343
* does not affect buffer's state and [exhausted] only indicates that a buffer is empty.
44+
*
45+
* ### Thread-safety guarantees
46+
*
47+
* [Buffer] does not provide any thread-safety guarantees.
48+
* If a [Buffer] needs to be accessed from multiple threads, an additional synchronization is required.
49+
* Failure to do so will result in possible data corruption, loss, and runtime errors.
4450
*/
4551
public class Buffer : Source, Sink {
4652
@PublishedApi

core/common/src/RawSink.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ package kotlinx.io
3232
*
3333
* Implementors should abstain from throwing exceptions other than those that are documented for RawSink methods.
3434
*
35+
* ### Thread-safety guarantees
36+
*
37+
* [RawSink] implementations are not required to be thread safe.
38+
* However, if an implementation provides some thread safety guarantees, it is recommended to explicitly document them.
39+
*
3540
* @sample kotlinx.io.samples.Crc32Sample.crc32
3641
*/
3742
public expect interface RawSink : AutoCloseable {

core/common/src/RawSource.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ package kotlinx.io
3232
*
3333
* Implementors should abstain from throwing exceptions other than those that are documented for RawSource methods.
3434
*
35+
* ### Thread-safety guarantees
36+
*
37+
* [RawSource] implementations are not required to be thread safe.
38+
* However, if an implementation provides some thread safety guarantees, it is recommended to explicitly document them.
39+
*
3540
* @sample kotlinx.io.samples.RC4SourceSample.rc4
3641
*/
3742
public interface RawSource : AutoCloseable {

core/common/src/Sink.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
44
*/
55

@@ -50,6 +50,11 @@ package kotlinx.io
5050
* Methods fully consuming its argument are named `transferFrom`, like [transferFrom].
5151
*
5252
* It is recommended to follow the same naming convention for Sink extensions.
53+
*
54+
* ### Thread-safety guarantees
55+
*
56+
* Until stated otherwise, [Sink] implementations are not thread safe.
57+
* If a [Sink] needs to be accessed from multiple threads, an additional synchronization is required.
5358
*/
5459
public sealed interface Sink : RawSink {
5560
/**

core/common/src/Source.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
44
*/
55

@@ -58,6 +58,11 @@ package kotlinx.io
5858
* Methods moving all data from a source to some other sink are named `transferTo`, like [transferTo].
5959
*
6060
* It is recommended to follow the same naming convention for Source extensions.
61+
*
62+
* ### Thread-safety guarantees
63+
*
64+
* Until stated otherwise, [Source] implementations are not thread safe.
65+
* If a [Source] needs to be accessed from multiple threads, an additional synchronization is required.
6166
*/
6267
public sealed interface Source : RawSource {
6368
/**
@@ -229,6 +234,12 @@ public sealed interface Source : RawSource {
229234
*
230235
* Peek could be used to lookahead and read the same data multiple times.
231236
*
237+
* If peek source needs to access more data that this [Source] has in its buffer,
238+
* more data will be requested from the underlying source and on success,
239+
* it'll be added to the buffer of this [Source].
240+
* If the underlying source was exhausted or some error occurred on attempt to fill the buffer,
241+
* a corresponding exception will be thrown.
242+
*
232243
* @throws IllegalStateException when the source is closed.
233244
*
234245
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.peekSample

core/common/src/files/FileSystem.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
44
*/
55

@@ -21,6 +21,11 @@ import kotlinx.io.RawSource
2121
* access to some network resources and allow working with them as with regular files, for example.
2222
*
2323
* **This API is unstable and subject to change.**
24+
*
25+
* ### Thread-safety guarantees
26+
*
27+
* Until stated otherwise, [FileSystem] implementation are not thread safe.
28+
* If a [FileSystem] needs to be accessed from multiple threads, an additional synchronization is required.
2429
*/
2530
public sealed interface FileSystem {
2631
/**
@@ -170,6 +175,9 @@ internal abstract class SystemFileSystemImpl : FileSystem
170175

171176
/**
172177
* An instance of [FileSystem] representing a default system-wide filesystem.
178+
*
179+
* *For `js` target, `SystemFileSystem` is only supported in `nodeJs` environment. Attempts to use it in `browser`
180+
* environment will result in runtime exception being thrown.*
173181
*/
174182
public expect val SystemFileSystem: FileSystem
175183

0 commit comments

Comments
 (0)