1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Locking related type

use cryptoki_sys::{CKF_OS_LOCKING_OK, CK_FLAGS};

use std::ptr;

/// Argument for the initialize function
#[derive(Copy, Clone, Debug)]
pub enum CInitializeArgs {
    /// The library can use the native OS library for locking
    OsThreads,
    // TODO: add variants for custom mutexes here and no multithreading, safety implications for
    // that.
}

impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
    fn from(c_initialize_args: CInitializeArgs) -> Self {
        let mut flags = CK_FLAGS::default();
        match c_initialize_args {
            CInitializeArgs::OsThreads => {
                flags |= CKF_OS_LOCKING_OK;
                Self {
                    flags,
                    CreateMutex: None,
                    DestroyMutex: None,
                    LockMutex: None,
                    UnlockMutex: None,
                    pReserved: ptr::null_mut(),
                }
            }
        }
    }
}