Skip to content

Commit 035c8ad

Browse files
committed
make thread-safe SOC backwards compatible
1 parent 730e2a2 commit 035c8ad

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

libctru/include/3ds/services/soc.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// The config level to be used with @ref SOCU_GetNetworkOpt
1313
#define SOL_CONFIG 0xfffe
1414

15-
/// Default number of SOC sessions to initialize
16-
#define SOC_DEFAULT_NUM_SESSIONS 3
15+
/// Default number of SOC sessions to initialize, for backwards compatibility
16+
#define SOC_DEFAULT_NUM_SESSIONS 1
1717

1818
/// Options to be used with @ref SOCU_GetNetworkOpt
1919
typedef enum
@@ -104,10 +104,19 @@ typedef struct
104104
* @brief Initializes the SOC service.
105105
* @param context_addr Address of a page-aligned (0x1000) buffer to be used.
106106
* @param context_size Size of the buffer, a multiple of 0x1000.
107-
* @param num_sessions The number of sessions to initialize. This value determines how many socket syscalls can be run concurrently. Setting it to 1 means only one thread can safely use socket syscalls.
108107
* @note The specified context buffer can no longer be accessed by the process which called this function, since the userland permissions for this block are set to no-access.
108+
* @note The SOC service can only be used in a single thread when using this function to initialize SOC services. In order to enable usage across multiple threads, use @ref socInitMulti, otherwise threads will block when using socket functions concurrently.
109109
*/
110-
Result socInit(u32 *context_addr, u32 context_size, u32 num_sessions);
110+
Result socInit(u32 *context_addr, u32 context_size);
111+
112+
/**
113+
* @brief Initializes the SOC service for use across multiple threads.
114+
* @param context_addr Address of a page-aligned (0x1000) buffer to be used.
115+
* @param context_size Size of the buffer, a multiple of 0x1000.
116+
* @param num_sessions The number of sessions to initialize. This value determines how many socket functions can be run concurrently. Setting it to 1 means only one thread can safely use socket functions.
117+
* @note The specified context buffer can no longer be accessed by the process which called this function, since the userland permissions for this block are set to no-access.
118+
*/
119+
Result socInitMulti(u32 *context_addr, u32 context_size, u32 num_sessions);
111120

112121
/**
113122
* @brief Closes the soc service.

libctru/source/services/soc/soc_init.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static Result SOCU_Shutdown(void)
228228
return cmdbuf[1];
229229
}
230230

231-
Result socInit(u32* context_addr, u32 context_size, u32 num_sessions)
231+
Result socInitMulti(u32* context_addr, u32 context_size, u32 num_sessions)
232232
{
233233
Result ret = 0;
234234

@@ -270,6 +270,11 @@ Result socInit(u32* context_addr, u32 context_size, u32 num_sessions)
270270
return 0;
271271
}
272272

273+
Result socInit(u32* context_addr, u32 context_size)
274+
{
275+
return socInitMulti(context_addr, context_size, SOC_DEFAULT_NUM_SESSIONS);
276+
}
277+
273278
Result socExit(void)
274279
{
275280
Result ret = 0;

0 commit comments

Comments
 (0)