Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/nxflat/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ SUBDIRS += longjmp
endif

ifneq ($(CONFIG_DISABLE_PTHREAD),y)
SUBDIRS += mutex pthread
SUBDIRS += mutex pthread tls
ifeq ($(CONFIG_HAVE_CXX),y)
SUBDIRS += tlsxx
endif
endif

ifneq ($(CONFIG_ARCH_ADDRENV),y)
Expand Down
4 changes: 4 additions & 0 deletions examples/nxflat/tests/tls/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tls
*.r1
*.r2
*-thunk.S
70 changes: 70 additions & 0 deletions examples/nxflat/tests/tls/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
############################################################################
# apps/examples/nxflat/tests/tls/Makefile
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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
#
# http://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 $(APPDIR)/Make.defs # Basic make _info

BIN = tls

R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)

R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)

all: $(BIN)

$(R1OBJS): %.o: %.c
@echo "CC: $<"
$(Q) $(CC) -c $(CPICFLAGS) $< -o $@

$(R2OBJ): %.o: %.S
@echo "AS: $<"
$(Q) $(CC) -c $(CPICFLAGS) $< -o $@

$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS1) -o $@ $^

$(R2SRC): $(BIN).r1
@echo "MK: $<"
$(Q) $(MKNXFLAT) -o $@ $^

$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)

$(BIN): $(BIN).r2
@echo "LD: $<"
$(Q) $(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^

$(ROMFS_DIR)/$(BIN): $(BIN)
$(Q) mkdir -p $(ROMFS_DIR)
$(Q) install -m 0755 $(BIN) $(ROMFS_DIR)/$(BIN)

install: $(ROMFS_DIR)/$(BIN)

clean:
$(call DELFILE, $(BIN))
$(call DELFILE, $(R2SRC))
$(call DELFILE, *.r1)
$(call DELFILE, *.r2)
$(call CLEAN)
148 changes: 148 additions & 0 deletions examples/nxflat/tests/tls/tls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/****************************************************************************
* apps/examples/nxflat/tests/tls/tls.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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
*
* http://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.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

/****************************************************************************
* Private Types
****************************************************************************/

struct tls_s
{
const char *name;
int *ptr;
int n;
};

/****************************************************************************
* Public Data
****************************************************************************/

int g_global = 117;

/* No initial value: no template. An initial value: a template. A struct
* with pointers: a template that the loader must relocate.
*/

__thread int g_zero;
__thread int g_init = 42;
__thread struct tls_s g_struct =
{
"tls-string", &g_global, 3
};

/****************************************************************************
* Private Data
****************************************************************************/

static int g_errors;
static int *g_childaddr;

/****************************************************************************
* Private Functions
****************************************************************************/

static void check(const char *who, const char *what, int ok)
{
printf("%s: %-34s %s\n", who, what, ok ? "PASS" : "FAIL");
if (!ok)
{
g_errors++;
}
}

static void check_initial(const char *who)
{
printf("%s: g_zero=%d g_init=%d name=%s *ptr=%d n=%d\n", who, g_zero,
g_init, g_struct.name, *g_struct.ptr, g_struct.n);

check(who, "zero-initialized variable", g_zero == 0);
check(who, "variable with a template", g_init == 42);
check(who, "string pointer in a template",
strcmp(g_struct.name, "tls-string") == 0);
check(who, "data pointer in a template", g_struct.ptr == &g_global);
check(who, "value through that pointer", *g_struct.ptr == 117);
check(who, "integer in a struct template", g_struct.n == 3);
}

/****************************************************************************
* Public Functions
****************************************************************************/

/* A function that is used through a pointer must have global scope: see
* the pthread test.
*/

void *child_start_routine(void *arg)
{
check_initial("CHILD");

g_childaddr = &g_zero;
g_zero = 2;
g_init = 102;
g_struct.n = 302;

check("CHILD", "own values read back",
g_zero == 2 && g_init == 102 && g_struct.n == 302);
return NULL;
}

int main(int argc, char **argv)
{
pthread_t thread;
int status;

check_initial("MAIN");

g_zero = 1;
g_init = 101;
g_struct.n = 301;

status = pthread_create(&thread, NULL, child_start_routine, NULL);
if (status != 0)
{
printf("MAIN: pthread_create() returned %d\n", status);
return EXIT_FAILURE;
}

pthread_join(thread, NULL);

check("MAIN", "values kept after the child ran",
g_zero == 1 && g_init == 101 && g_struct.n == 301);
check("MAIN", "child has its own copy", g_childaddr != &g_zero);

if (g_errors != 0)
{
printf("TLS test FAILED: %d error(s)\n", g_errors);
return EXIT_FAILURE;
}

printf("TLS test PASSED\n");
return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions examples/nxflat/tests/tlsxx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tlsxx
*.r1
*.r2
*-thunk.S
70 changes: 70 additions & 0 deletions examples/nxflat/tests/tlsxx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
############################################################################
# apps/examples/nxflat/tests/tlsxx/Makefile
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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
#
# http://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 $(APPDIR)/Make.defs # Basic make _info

BIN = tlsxx

R1SRCS = $(BIN).cxx
R1OBJS = $(R1SRCS:.cxx=.o)

R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)

all: $(BIN)

$(R1OBJS): %.o: %.cxx
@echo "CXX: $<"
$(Q) $(CXX) -c $(CXXPICFLAGS) $< -o $@

$(R2OBJ): %.o: %.S
@echo "AS: $<"
$(Q) $(CC) -c $(CPICFLAGS) $< -o $@

$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS1) -o $@ $^

$(R2SRC): $(BIN).r1
@echo "MK: $<"
$(Q) $(MKNXFLAT) -o $@ $^

$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)

$(BIN): $(BIN).r2
@echo "LD: $<"
$(Q) $(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^

$(ROMFS_DIR)/$(BIN): $(BIN)
$(Q) mkdir -p $(ROMFS_DIR)
$(Q) install -m 0755 $(BIN) $(ROMFS_DIR)/$(BIN)

install: $(ROMFS_DIR)/$(BIN)

clean:
$(call DELFILE, $(BIN))
$(call DELFILE, $(R2SRC))
$(call DELFILE, *.r1)
$(call DELFILE, *.r2)
$(call CLEAN)
Loading
Loading