summaryrefslogtreecommitdiff
path: root/bsp/xuantie/libraries/xuantie_libraries/smp_demo.c
blob: e7916a44e5fc8303a4fc6e4051f1d3ab7442c1e7 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright (C) 2017-2024 Alibaba Group Holding Limited
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <rtthread.h>
#include <rthw.h>

static int g_count;
rt_hw_spinlock_t spinlock;

static void thread_entry(void *parameter)
{
    int id;

    while (1) {
        id = rt_hw_cpu_id();
        rt_hw_spin_lock(&spinlock);
        printf("[%s] in %d core, count:%d \r\n", rt_thread_self()->parent.name, id, g_count++);
        rt_hw_spin_unlock(&spinlock);
        rt_thread_mdelay(1000);
    }
}

void smp_example(void)
{
    printf("smp demo start!\r\n");
    rt_hw_spin_lock_init(&spinlock);
    for (long i = 0; i < CONFIG_NR_CPUS; i++) {
        char buf[32];
        rt_thread_t tid;

        snprintf(buf, sizeof(buf), "thread_%ld", i);
        tid = rt_thread_create(buf, thread_entry, RT_NULL, CONFIG_APP_TASK_STACK_SIZE, 10, 5);
        if (tid == RT_NULL) {
            printf("create %s failed!\r\n", buf);
            continue;
        }
        rt_thread_control(tid, RT_THREAD_CTRL_BIND_CPU, (void *)i);
        rt_thread_startup(tid);
    }
}